Skip to content
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

enhance: add support for streaming with o1 model #461

Merged
merged 1 commit into from
Feb 27, 2025
Merged
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
3 changes: 1 addition & 2 deletions openai-model-provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func main() {

openaiProxy := openaiproxy.NewServer(cfg)
reverseProxy := &httputil.ReverseProxy{
Director: openaiProxy.Openaiv1ProxyRedirect,
ModifyResponse: openaiProxy.ModifyResponse,
Director: openaiProxy.Openaiv1ProxyRedirect,
}
cfg.CustomPathHandleFuncs["/v1/"] = reverseProxy.ServeHTTP

Expand Down
62 changes: 0 additions & 62 deletions openai-model-provider/openaiproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func (s *Server) Openaiv1ProxyRedirect(req *http.Request) {
}

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" {
Expand All @@ -63,67 +62,6 @@ func modifyRequestBodyForO1(req *http.Request, reqBody *openai.ChatCompletionReq
}
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
}

Expand Down
Loading