-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chore: Add openai o1 support #366
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f09a61f
Chore: Add openai o1 support
StrongMonkey 91dc14d
Update go.mod tidy
StrongMonkey ad24333
Add proxy change to return o1 response as SSE events
StrongMonkey 96e4ba3
Address donnie's comment
StrongMonkey 46eee43
Use custom path
StrongMonkey 27cf37c
Address comment
StrongMonkey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789 h1:rfriXe+FFqZ5fZ+wGzLUivrq7Fyj2xfRdZjDsHf6Ps0= | ||
github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789 h1:rfriXe+FFqZ5fZ+wGzLUivrq7Fyj2xfRdZjDsHf6Ps0= | ||
github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789 h1:rfriXe+FFqZ5fZ+wGzLUivrq7Fyj2xfRdZjDsHf6Ps0= | ||
github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/obot-platform/tools/openai-model-provider | ||
|
||
go 1.23.4 | ||
|
||
require github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789 h1:rfriXe+FFqZ5fZ+wGzLUivrq7Fyj2xfRdZjDsHf6Ps0= | ||
github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package openaiproxy | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
openai "github.com/gptscript-ai/chat-completion-client" | ||
"github.com/obot-platform/tools/openai-model-provider/proxy" | ||
) | ||
|
||
type Server struct { | ||
cfg *proxy.Config | ||
} | ||
|
||
func NewServer(cfg *proxy.Config) *Server { | ||
return &Server{cfg: cfg} | ||
} | ||
|
||
func (s *Server) Openaiv1ProxyRedirect(req *http.Request) { | ||
req.URL.Scheme = s.cfg.URL.Scheme | ||
req.URL.Host = s.cfg.URL.Host | ||
req.URL.Path = s.cfg.URL.JoinPath(strings.TrimPrefix(req.URL.Path, "/v1")).Path // join baseURL with request path - /v1 must be part of baseURL if it's needed | ||
req.Host = req.URL.Host | ||
|
||
req.Header.Set("Authorization", "Bearer "+s.cfg.APIKey) | ||
|
||
if req.Body == nil || s.cfg.URL.Host != proxy.OpenaiBaseHostName || req.URL.Path != proxy.ChatCompletionsPath { | ||
return | ||
} | ||
|
||
bodyBytes, err := io.ReadAll(req.Body) | ||
if err != nil { | ||
fmt.Println("failed to read request body, error: ", err.Error()) | ||
return | ||
} | ||
|
||
var reqBody openai.ChatCompletionRequest | ||
if err := json.Unmarshal(bodyBytes, &reqBody); err == nil && isModelO1(reqBody.Model) { | ||
if err := modifyRequestBodyForO1(req, &reqBody); err != nil { | ||
fmt.Println("failed to modify request body for o1, error: ", err.Error()) | ||
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) | ||
} | ||
} else { | ||
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) | ||
} | ||
} | ||
|
||
func modifyRequestBodyForO1(req *http.Request, reqBody *openai.ChatCompletionRequest) error { | ||
reqBody.Stream = false | ||
reqBody.Temperature = nil | ||
for i, msg := range reqBody.Messages { | ||
if msg.Role == "system" { | ||
reqBody.Messages[i].Role = "developer" | ||
} | ||
} | ||
modifiedBodyBytes, err := json.Marshal(reqBody) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal request body after modification: %w", err) | ||
} | ||
req.Body = io.NopCloser(bytes.NewBuffer(modifiedBodyBytes)) | ||
req.ContentLength = int64(len(modifiedBodyBytes)) | ||
req.Header.Set("Accept", "application/json") | ||
req.Header.Set("Accept-Encoding", "") | ||
req.Header.Set("Content-Type", "application/json") | ||
return nil | ||
} | ||
|
||
func (s *Server) ModifyResponse(resp *http.Response) error { | ||
if resp.StatusCode != http.StatusOK || resp.Request.URL.Path != proxy.ChatCompletionsPath || resp.Request.URL.Host != proxy.OpenaiBaseHostName { | ||
return nil | ||
} | ||
|
||
if resp.Header.Get("Content-Type") == "application/json" { | ||
rawBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
resp.Body.Close() | ||
return fmt.Errorf("failed to read response body: %w", err) | ||
} | ||
resp.Body.Close() | ||
var respBody openai.ChatCompletionResponse | ||
if err := json.Unmarshal(rawBody, &respBody); err == nil && isModelO1(respBody.Model) { | ||
// Convert non-streaming response to a single SSE for o1 model | ||
streamResponse := openai.ChatCompletionStreamResponse{ | ||
ID: respBody.ID, | ||
Object: respBody.Object, | ||
Created: respBody.Created, | ||
Model: respBody.Model, | ||
Usage: respBody.Usage, | ||
Choices: func() []openai.ChatCompletionStreamChoice { | ||
var choices []openai.ChatCompletionStreamChoice | ||
for _, choice := range respBody.Choices { | ||
choices = append(choices, openai.ChatCompletionStreamChoice{ | ||
Index: choice.Index, | ||
Delta: openai.ChatCompletionStreamChoiceDelta{ | ||
Content: choice.Message.Content, | ||
Role: choice.Message.Role, | ||
FunctionCall: choice.Message.FunctionCall, | ||
ToolCalls: choice.Message.ToolCalls, | ||
}, | ||
FinishReason: choice.FinishReason, | ||
}) | ||
} | ||
return choices | ||
}(), | ||
} | ||
|
||
sseData, err := json.Marshal(streamResponse) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal stream response: %w", err) | ||
} | ||
|
||
sseFormattedData := fmt.Sprintf("data: %s\n\nevent: close\ndata: [DONE]\n\n", sseData) | ||
|
||
resp.Header.Set("Content-Type", "text/event-stream") | ||
resp.Header.Set("Cache-Control", "no-cache") | ||
resp.Header.Set("Connection", "keep-alive") | ||
resp.Body = io.NopCloser(bytes.NewBufferString(sseFormattedData)) | ||
} else { | ||
resp.Body = io.NopCloser(bytes.NewBuffer(rawBody)) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func isModelO1(model string) bool { | ||
if model == "o1" { | ||
return true | ||
} | ||
return strings.HasPrefix(model, "o1-") && !strings.HasPrefix(model, "o1-mini") && !strings.HasPrefix(model, "o1-preview") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iwilltry42 can you review this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
I don't like all those if-else statements, but have no cleaner solution right now (unfortunately, mux doesn't take care of the path sorting and neither allows overrides nor looking up what paths are already handled.