From ddbe908873153e376c2b687c20e51f3e4774ffc3 Mon Sep 17 00:00:00 2001 From: tkisme Date: Fri, 17 May 2024 15:05:22 +0800 Subject: [PATCH] add cf --- config.json | 2 +- main.go | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/config.json b/config.json index 27a4db5..60bceb9 100644 --- a/config.json +++ b/config.json @@ -2,7 +2,7 @@ "bind": "127.0.0.1:8181", "proxy_url": "", "timeout": 600, - "codex_api_base": "https://api-proxy.oaipro.com/v1", + "codex_api_base": "https://api-proxy.oaipro.com/v1/completions", "codex_api_key": "sk-xxx", "codex_api_organization": "", "codex_api_project": "", diff --git a/main.go b/main.go index 56d3e70..e6f845a 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "net/http" "net/url" "os" + "strings" "time" ) @@ -220,9 +221,19 @@ func (s *ProxyService) codeCompletions(c *gin.Context) { body, _ = sjson.DeleteBytes(body, "extra") body, _ = sjson.DeleteBytes(body, "nwo") - body, _ = sjson.SetBytes(body, "model", "gpt-3.5-turbo-instruct") + body, _ = sjson.SetBytes(body, "model", s.cfg.CodexModelDefault) - proxyUrl := s.cfg.CodexApiBase + "/completions" + proxyUrl := s.cfg.CodexApiBase + if strings.HasPrefix(s.cfg.CodexModelDefault, "@") { + proxyUrl = s.cfg.CodexApiBase + message := gjson.GetBytes(body, "prompt").String() + body, _ = sjson.DeleteBytes(body, "prompt") + msg := make([]GPTMessage, 0) + msg = append(msg, GPTMessage{Role: "system", Content: "You are a helpful assistant"}) + msg = append(msg, GPTMessage{Role: "user", Content: message}) + body, _ = sjson.SetBytes(body, "messages", msg) + body, _ = sjson.DeleteBytes(body, "n") + } req, err := http.NewRequestWithContext(ctx, http.MethodPost, proxyUrl, io.NopCloser(bytes.NewBuffer(body))) if nil != err { abortCodex(c, http.StatusInternalServerError)