Skip to content

Commit

Permalink
Chore: Add openai o1 support
Browse files Browse the repository at this point in the history
Signed-off-by: Daishan Peng <[email protected]>
  • Loading branch information
StrongMonkey committed Jan 23, 2025
1 parent af5a039 commit 5bfd979
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions openai-model-provider/go.mod
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
2 changes: 2 additions & 0 deletions openai-model-provider/go.sum
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=
40 changes: 40 additions & 0 deletions openai-model-provider/proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package proxy

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httputil"
"strings"

openai "github.com/gptscript-ai/chat-completion-client"
)

type Config struct {
Expand Down Expand Up @@ -96,6 +101,41 @@ func (s *server) proxyDirector(req *http.Request) {
if s.cfg.PathPrefix != "" && !strings.HasPrefix(req.URL.Path, s.cfg.PathPrefix) {
req.URL.Path = s.cfg.PathPrefix + req.URL.Path
}

if req.Body == nil || req.Method != http.MethodPost {
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
// ignore errors here, because the request can be something other than ChatCompletionRequest
if err := json.Unmarshal(bodyBytes, &reqBody); err == nil && reqBody.Model == "o1" {
modifyRequestBodyForO1(req, &reqBody)
} else {
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
}
}

func modifyRequestBodyForO1(req *http.Request, reqBody *openai.ChatCompletionRequest) {
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 {
req.Body = io.NopCloser(bytes.NewBuffer(modifiedBodyBytes))
req.ContentLength = int64(len(modifiedBodyBytes))
} else {
fmt.Println("failed to marshal request body after modification and skipping, error: ", err.Error())
}
}

func Validate(cfg *Config) error {
Expand Down

0 comments on commit 5bfd979

Please sign in to comment.