From 9e33d22c2357ec984e1fbe1a972d71fc1182b4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?c=E8=8F=8C?= Date: Sat, 16 Sep 2023 20:53:50 +0800 Subject: [PATCH] up --- pkg/claude/claude.go | 213 ++++--------------------------------------- 1 file changed, 16 insertions(+), 197 deletions(-) diff --git a/pkg/claude/claude.go b/pkg/claude/claude.go index 5b99a38..cbce93e 100644 --- a/pkg/claude/claude.go +++ b/pkg/claude/claude.go @@ -323,6 +323,22 @@ func TransRsp(c *gin.Context, isStream bool, reader *bufio.Reader) { dataChan <- string(bytedate) } } else { + chatrsp := openai.ChatCompletionStreamResponse{ + ID: result.LogID, + Model: result.Model, + Object: "chat.completion", + Created: time.Now().Unix(), + } + choice := openai.ChatCompletionStreamChoice{ + Delta: openai.ChatCompletionStreamChoiceDelta{ + Role: "assistant", + Content: result.Completion, + }, + } + choice.FinishReason = openai.FinishReason(TranslatestopReason(result.StopReason)) + chatrsp.Choices = append(chatrsp.Choices, choice) + bytedate, _ := json.Marshal(chatrsp) + dataChan <- string(bytedate) dataChan <- "[DONE]" break } @@ -359,203 +375,6 @@ func TransRsp(c *gin.Context, isStream bool, reader *bufio.Reader) { } } -func TranslateStream(ctx *gin.Context, reader *bufio.Reader, dataChan chan string, stopChan chan bool) { - // dataChan := make(chan string) - // stopChan := make(chan bool) - for { - line, err := reader.ReadString('\n') - if err == nil { - if strings.HasPrefix(line, "data: ") { - var result CompleteResponse - json.NewDecoder(strings.NewReader(line[6:])).Decode(&result) - if result.StopReason == "" { - if result.Completion != "" { - chatrsp := openai.ChatCompletionStreamResponse{ - ID: result.LogID, - Model: result.Model, - Object: "chat.completion", - Created: time.Now().Unix(), - } - choice := openai.ChatCompletionStreamChoice{ - Delta: openai.ChatCompletionStreamChoiceDelta{ - Role: "assistant", - Content: result.Completion, - }, - FinishReason: "", - } - chatrsp.Choices = append(chatrsp.Choices, choice) - bytedate, _ := json.Marshal(chatrsp) - dataChan <- string(bytedate) - } - } else { - dataChan <- "data: [DONE]" - break - } - } else { - continue - } - } else { - log.Println("err:", err) - break - } - } - -} - -func Translate(c *gin.Context, chatreq *openai.ChatCompletionRequest) { - transReq := CompleteRequest{ - Model: chatreq.Model, - Temperature: int(chatreq.Temperature), - TopP: int(chatreq.TopP), - Stream: chatreq.Stream, - MaxTokensToSample: chatreq.MaxTokens, - } - var prompt string - for _, msg := range chatreq.Messages { - switch msg.Role { - case "system": - prompt += fmt.Sprintf("\n\nSystem:%s", msg.Content) - case "user": - prompt += fmt.Sprintf("\n\nUser:%s", msg.Content) - case "assistant": - prompt += fmt.Sprintf("\n\nAssistant:%s", msg.Content) - } - } - transReq.Prompt = prompt + "\n\nAssistant:" - - key, err := store.SelectKeyCache("anthropic") - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": gin.H{ - "message": err.Error(), - }, - }) - return - } - var payload = bytes.NewBuffer(nil) - if err := json.NewEncoder(payload).Encode(transReq); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": gin.H{ - "message": err.Error(), - }, - }) - return - } - - req, _ := http.NewRequest("POST", ClaudeUrl, payload) - - req.Header.Add("accept", "application/json") - req.Header.Add("anthropic-version", "2023-06-01") - req.Header.Add("x-api-key", key.Key) - req.Header.Add("content-type", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": gin.H{ - "message": err.Error(), - }, - }) - return - } - defer resp.Body.Close() - - for name, values := range resp.Header { - for _, value := range values { - c.Writer.Header().Add(name, value) - } - } - head := map[string]string{ - "Cache-Control": "no-store", - "access-control-allow-origin": "*", - "access-control-allow-credentials": "true", - } - for k, v := range head { - if _, ok := resp.Header[k]; !ok { - c.Writer.Header().Set(k, v) - } - } - resp.Header.Del("content-security-policy") - resp.Header.Del("content-security-policy-report-only") - resp.Header.Del("clear-site-data") - - c.Writer.WriteHeader(resp.StatusCode) - writer := bufio.NewWriter(c.Writer) - defer writer.Flush() - reader := bufio.NewReader(resp.Body) - - if resp.StatusCode == 200 { - if chatreq.Stream { - contentCh := fetchResponseContent(c, reader) - - for content := range contentCh { - writer.WriteString(content) - writer.WriteString("\n\n") - writer.Flush() - } - writer.WriteString("[DONE]") - writer.Flush() - } else { - return - } - } -} - -func fetchResponseContent(ctx *gin.Context, responseBody *bufio.Reader) <-chan string { - contentCh := make(chan string) - go func() { - defer close(contentCh) - for { - line, err := responseBody.ReadString('\n') - if err == nil { - lines := strings.Split(line, "") - for _, word := range lines { - ctx.Writer.WriteString(word) - ctx.Writer.Flush() - } - if line == "\n" { - continue - } - if strings.HasPrefix(line, "data:") { - line = strings.TrimSpace(strings.TrimPrefix(line, "data:")) - if strings.HasSuffix(line, "[DONE]") { - break - } - line = strings.TrimSpace(line) - } - - dec := json.NewDecoder(strings.NewReader(line)) - var data map[string]interface{} - if err := dec.Decode(&data); err == io.EOF { - log.Println("EOF:", err) - break - } else if err != nil { - fmt.Println("Error decoding response:", err) - return - } - if stop_reason, ok := data["stop_reason"].(string); ok { - if stop_reason != "" { - break - } else { - result := data["completion"].(string) - choice := openai.ChatCompletionStreamChoice{ - Delta: openai.ChatCompletionStreamChoiceDelta{ - Role: "assistant", - Content: result, - }, - } - bytedate, _ := json.Marshal(choice) - contentCh <- "data:" + string(bytedate) - } - } - } else { - break - } - } - }() - return contentCh -} - // claude -> openai func TranslatestopReason(reason string) string { switch reason {