8000 enhance: forward OpenAI computer-use models to responses API by thedadams · Pull Request #531 · obot-platform/tools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

enhance: forward OpenAI computer-use models to responses API #531

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions deepseek-model-provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ go 1.23.4
replace github.com/obot-platform/tools/openai-model-provider => ../openai-model-provider

require github.com/obot-platform/tools/openai-model-provider v0.0.0

require github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789 // indirect
2 changes: 0 additions & 2 deletions deepseek-model-provider/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
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=
2 changes: 0 additions & 2 deletions groq-model-provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ go 1.23.4
replace github.com/obot-platform/tools/openai-model-provider => ../openai-model-provider

require github.com/obot-platform/tools/openai-model-provider v0.0.0

require github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789 // indirect
2 changes: 0 additions & 2 deletions groq-model-provider/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
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=
2 changes: 0 additions & 2 deletions ollama-model-provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ go 1.23.4
replace github.com/obot-platform/tools/openai-model-provider => ../openai-model-provider

require github.com/obot-platform/tools/openai-model-provider v0.0.0

require github.com/gptscript-ai/chat-completion-client v0.0.0-20250123123106-c86554320789 // indirect
2 changes: 0 additions & 2 deletions ollama-model-provider/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
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=
12 changes: 11 additions & 1 deletion openai-model-provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ 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
require (
github.com/gptscript-ai/chat-completion-client v0.0.0-20250224164718-139cb4507b1d
github.com/openai/openai-go v0.1.0-beta.2
github.com/tidwall/sjson v1.2.5
)

require (
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)
16 changes: 14 additions & 2 deletions openai-model-provider/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
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=
github.com/gptscript-ai/chat-completion-client v0.0.0-20250224164718-139cb4507b1d h1:p5uqZufDIMQzAALblZFkr8fwbnZbFXbBCR1ZMAFylXk=
github.com/gptscript-ai/chat-completion-client v0.0.0-20250224164718-139cb4507b1d/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo=
github.com/openai/openai-go v0.1.0-beta.2 h1:Ra5nCFkbEl9w+UJwAciC4kqnIBUCcJazhmMA0/YN894=
github.com/openai/openai-go v0.1.0-beta.2/go.mod h1:g461MYGXEXBVdV5SaR/5tNzNbSfwTBBefwc+LlDCK0Y=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
298 changes: 288 additions & 10 deletions openai-model-provider/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package main

import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"os"
"strings"

gopenai "github.com/gptscript-ai/chat-completion-client"
"github.com/obot-platform/tools/openai-model-provider/openaiproxy"
"github.com/obot-platform/tools/openai-model-provider/proxy"
"github.com/openai/openai-go"
"github.com/openai/openai-go/packages/param"
"github.com/openai/openai-go/responses"
"github.com/openai/openai-go/shared"
"github.com/openai/openai-go/shared/constant"
"github.com/tidwall/sjson"
)

func main() {
Expand All @@ -23,19 +35,16 @@ func main() {
}

cfg := &proxy.Config{
APIKey: apiKey,
ListenPort: port,
BaseURL: "https://api.openai.com/v1",
RewriteModelsFn: proxy.DefaultRewriteModelsResponse,
Name: "OpenAI",
CustomPathHandleFuncs: map[string]http.HandlerFunc{},
APIKey: apiKey,
ListenPort: port,
BaseURL: "https://api.openai.com/v1",
RewriteModelsFn: proxy.DefaultRewriteModelsResponse,
Name: "OpenAI",
}

openaiProxy := openaiproxy.NewServer(cfg)
reverseProxy := &httputil.ReverseProxy{
Director: openaiProxy.Openaiv1ProxyRedirect,
cfg.CustomPathHandleFuncs = map[string]http.HandlerFunc{
"/v1/": translateResponsesAPI(apiKey, openaiproxy.NewServer(cfg)),
}
cfg.CustomPathHandleFuncs["/v1/"] = reverseProxy.ServeHTTP

if len(os.Args) > 1 && os.Args[1] == "validate" {
if err := cfg.Validate("/tools/openai-model-provider/validate"); err != nil {
Expand All @@ -48,3 +57,272 @@ func main() {
panic(err)
}
}

type responsesRequestTranslator struct {
openAIProxy *openaiproxy.Server
apiKey string
wasTranslated bool
streaming bool
}

func translateResponsesAPI(apiKey string, openAIProxy *openaiproxy.Server) func(rw http.ResponseWriter, req *http.Request) {
fmt.Println("Translating responses API request")
return func(rw http.ResponseWriter, req *http.Request) {
r := &responsesRequestTranslator{apiKey: apiKey, openAIProxy: openAIProxy}
(&httputil.ReverseProxy{
Director: r.openaiProxyWithComputerUse,
ModifyResponse: r.modifyResponsesAPIResponse,
}).ServeHTTP(rw, req)
}
}

func (r *responsesRequestTranslator) openaiProxyWithComputerUse(req *http.Request) {
var contentLength int64
req.URL.Scheme = "https"
req.URL.Host = "api.openai.com"
req.Host = req.URL.Host
req.Body, req.URL.Path, contentLength = r.rewriteBody(req.Body, req.URL.Path)
if r.wasTranslated {
req.ContentLength = contentLength
req.Header.Set("Content-Length", fmt.Sprintf("%d", contentLength))
} else {
r.openAIProxy.Openaiv1ProxyRedirect(req)
}

req.Header.Set("Authorization", "Bearer "+r.apiKey)
}

func (r *responsesRequestTranslator) rewriteBody(body io.ReadCloser, path string) (io.ReadCloser, string, int64) {
if body == nil || path != proxy.ChatCompletionsPath {
fmt.Fprintf(os.Stderr, "Not a chat completion request, just returning original body and path\n")
// Not a chat completion request, just return the original body and path.
return body, path, 0
}

bodyBytes, err := io.ReadAll(body)
if err != nil {
// Best effort, just return the original body and path on error.
fmt.Fprintf(os.Stderr, "Failed to read request body: %v\n", err)
return body, path, 0
}
_ = body.Close()

var chatCompletionRequest gopenai.ChatCompletionRequest
if err := json.Unmarshal(bodyBytes, &chatCompletionRequest); err != nil {
// Best effort, just return the original body and path on error.
fmt.Fprintf(os.Stderr, "Failed to unmarshal chat completion request: %v\n", err)
return io.NopCloser(bytes.NewBuffer(bodyBytes)), path, 0
}
if !strings.HasPrefix(chatCompletionRequest.Model, "computer-use-") {
// Not a computer use model, just return the original body and path.
return io.NopCloser(bytes.NewBuffer(bodyBytes)), path, 0
}

var (
text responses.ResponseTextConfigParam
inputItems []responses.ResponseInputItemUnionParam
tools []responses.ToolUnionParam
)
// Translate the response format
if chatCompletionRequest.ResponseFormat != nil {
switch chatCompletionRequest.ResponseFormat.Type {
case gopenai.ChatCompletionResponseFormatTypeText:
text = responses.ResponseTextConfigParam{
Format: responses.ResponseFormatTextConfigUnionParam{
OfText: &shared.ResponseFormatTextParam{
Type: constant.Text(gopenai.ChatCompletionResponseFormatTypeText),
},
},
}
case gopenai.ChatCompletionResponseFormatTypeJSONObject:
text = responses.ResponseTextConfigParam{
Format: responses.ResponseFormatTextConfigUnionParam{
OfJSONObject: &shared.ResponseFormatJSONObjectParam{
Type: constant.JSONObject(gopenai.ChatCompletionResponseFormatTypeJSONObject),
},
},
}
default:
// Best effort log and move on.
fmt.Fprintf(os.Stderr, "Unsupported response format type: %v\n", chatCompletionRequest.ResponseFormat.Type)
}
}
// Translate the messages to input items
inputItems = make([]responses.ResponseInputItemUnionParam, 0, len(chatCompletionRequest.Messages))
for _, message := range chatCompletionRequest.Messages {
switch {
case len(message.ToolCalls) > 0:
for _, call := range message.ToolCalls {
inputItems = append(inputItems, responses.ResponseInputItemParamOfFunctionCall(
call.Function.Arguments,
call.ID,
call.Function.Name,
))
}
case message.Role == gopenai.ChatMessageRoleFunction:
inputItems = append(inputItems, responses.ResponseInputItemParamOfFunctionCallOutput(
message.ToolCallID,
message.Content,
))
case message.Role == gopenai.ChatMessageRoleUser || message.Role == gopenai.ChatMessageRoleAssistant || message.Role == gopenai.ChatMessageRoleSystem:
inputItems = append(inputItems, responses.ResponseInputItemParamOfMessage(
message.Content,
responses.EasyInputMessageRole(message.Role),
))
default:
// Best effort log and move on.
fmt.Fprintf(os.Stderr, "Unsupported message role: %v\n", message.Role)
}
}
// Translate the tools to tool union params
var parameters map[string]any
for _, tool := range chatCompletionRequest.Tools {
parameters, _ = tool.Function.Parameters.(map[string]any)
tools = append(tools, responses.ToolParamOfFunction(
tool.Function.Name,
parameters,
false,
))
}
// Translate the chat completion request to a responses API request
responsesRequest := responses.ResponseNewParams{
Input: responses.ResponseNewParamsInputUnion{
OfInputItemList: inputItems,
},
Model: shared.ResponsesModel(chatCompletionRequest.Model),
Instructions: openai.String(""),
MaxOutputTokens: openai.Int(int64(chatCompletionRequest.MaxTokens)),
ParallelToolCalls: openai.Bool(true),
PreviousResponseID: openai.String(""),
Store: openai.Bool(false),
Temperature: openai.Float(float64(*chatCompletionRequest.Temperature)),
TopP: openai.Float(float64(chatCompletionRequest.TopP)),
User: openai.String(chatCompletionRequest.User),
Reasoning: shared.ReasoningParam{},
Include: nil,
Metadata: nil,
Truncation: responses.ResponseNewParamsTruncationDisabled,
Text: text,
ToolChoice: responses.ResponseNewParamsToolChoiceUnion{
OfToolChoiceMode: param.Opt[responses.ToolChoiceOptions]{
Value: responses.ToolChoiceOptionsAuto,
},
},
Tools: tools,
}

// Marshal the responses request to JSON
responsesRequestBytes, err := json.MarshalIndent(responsesRequest, "", " ")
if err != nil {
// Best effort, just return the original body and path on error.
fmt.Fprintf(os.Stderr, "Failed to marshal responses request: %v\n", err)
return io.NopCloser(bytes.NewBuffer(bodyBytes)), path, 0
}

if chatCompletionRequest.Stream {
r.streaming = true
responsesRequestBytes, err = sjson.SetBytes(responsesRequestBytes, "stream", true)
if err != nil {
// Best effort, just return the original body and path on error.
fmt.Fprintf(os.Stderr, "Failed to set stream in responses request: %v\n", err)
return io.NopCloser(bytes.NewBuffer(bodyBytes)), path, 0
}
}

r.wasTranslated = true
// Return the new body and path
return io.NopCloser(bytes.NewBuffer(responsesRequestBytes)), "/v1/responses", int64(len(responsesRequestBytes))
}

func (r *responsesRequestTranslator) modifyResponsesAPIResponse(resp *http.Response) error {
if !r.wasTranslated || resp.StatusCode != http.StatusOK {
return nil
}

if !r.streaming {
return handleNonStreamingResponse(resp)
}

return nil
}

func handleNonStreamingResponse(resp *http.Response) error {
var responsesResponse responses.Response
var body io.Reader = resp.Body
if resp.Header.Get("Content-Encoding") == "gzip" {
gzReader, err := gzip.NewReader(resp.Body)
if err != nil {
return fmt.Errorf("failed to create gzip reader: %w", err)
}
defer gzReader.Close()
resp.Header.Del("Content-Encoding")
body = gzReader
}

if err := json.NewDecoder(body).Decode(&responsesResponse); err != nil {
return fmt.Errorf("failed to decode responses API response: %w", err)
}

choices := make([]gopenai.ChatCompletionChoice, 0, len(responsesResponse.Output))
for i, choice := range responsesResponse.Output {
switch choice.Type {
case "message":
// Convert all outputs
for _, content := range choice.Content {
switch content.Type {
case "output_text":
choices = append(choices, gopenai.ChatCompletionChoice{
Index: i,
Message: gopenai.ChatCompletionMessage{
Role: string(choice.Role),
Content: content.AsOutputText().Text,
},
FinishReason: gopenai.FinishReasonStop,
})
case "refusal":
choices = append(choices, gopenai.ChatCompletionChoice{
Index: i,
Message: gopenai.ChatCompletionMessage{
Role: string(choice.Role),
Content: content.AsRefusal().Refusal,
},
FinishReason: gopenai.FinishReasonContentFilter,
})
}
}
case "function_call":
choices = append(choices, gopenai.ChatCompletionChoice{
Index: i,
Message: gopenai.ChatCompletionMessage{
Role: string(choice.Role),
Content: choice.AsFunctionCall().Name,
},
FinishReason: gopenai.FinishReasonToolCalls,
})
}
}

chatCompletionResponse := gopenai.ChatCompletionResponse{
ID: responsesResponse.ID,
Object: "chat.completion",
Created: int64(responsesResponse.CreatedAt),
Model: responsesResponse.Model,
Choices: choices,
Usage: gopenai.Usage{
PromptTokens: int(responsesResponse.Usage.InputTokens),
CompletionTokens: int(responsesResponse.Usage.OutputTokens),
TotalTokens: int(responsesResponse.Usage.TotalTokens),
},
SystemFingerprint: "",
}

b, err := json.Marshal(chatCompletionResponse)
if err != nil {
return fmt.Errorf("failed to marshal chat completion response: %w", err)
}

resp.Body = io.NopCloser(bytes.NewReader(b))
resp.ContentLength = int64(len(b))
resp.Header.Set("Content-Length", fmt.Sprintf("%d", len(b)))
return nil
}
1 change: 1 addition & 0 deletions openai-model-provider/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (s *server) healthz(w http.ResponseWriter, _ *http.Request) {
}

func (s *server) proxyDirector(req *http.Request) {
fmt.Println("Proxying request to OpenAI the lame way")
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
Expand Down
Loading
Loading
0