|
| 1 | +// Copyright 2023 The casbin Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package model |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "net/http" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/Lok-Lu/go-openrouter" |
| 25 | + "github.com/casbin/casibase/proxy" |
| 26 | +) |
| 27 | + |
| 28 | +type OpenRouterModelProvider struct { |
| 29 | + subType string |
| 30 | + secretKey string |
| 31 | + siteName string |
| 32 | + siteUrl string |
| 33 | +} |
| 34 | + |
| 35 | +func NewOpenRouterModelProvider(subType string, secretKey string) (*OpenRouterModelProvider, error) { |
| 36 | + p := &OpenRouterModelProvider{ |
| 37 | + subType: subType, |
| 38 | + secretKey: secretKey, |
| 39 | + siteName: "Casibase", |
| 40 | + siteUrl: "https://casibase.org", |
| 41 | + } |
| 42 | + return p, nil |
| 43 | +} |
| 44 | + |
| 45 | +func (p *OpenRouterModelProvider) getProxyClientFromToken() *openrouter.Client { |
| 46 | + config, err := openrouter.DefaultConfig(p.secretKey, p.siteName, p.siteUrl) |
| 47 | + if err != nil { |
| 48 | + panic(err) |
| 49 | + } |
| 50 | + |
| 51 | + config.HTTPClient = proxy.ProxyHttpClient |
| 52 | + |
| 53 | + c := openrouter.NewClientWithConfig(config) |
| 54 | + return c |
| 55 | +} |
| 56 | + |
| 57 | +func (p *OpenRouterModelProvider) QueryText(question string, writer io.Writer, builder *strings.Builder) error { |
| 58 | + client := p.getProxyClientFromToken() |
| 59 | + |
| 60 | + ctx := context.Background() |
| 61 | + flusher, ok := writer.(http.Flusher) |
| 62 | + if !ok { |
| 63 | + return fmt.Errorf("writer does not implement http.Flusher") |
| 64 | + } |
| 65 | + |
| 66 | + model := p.subType |
| 67 | + if model == "" { |
| 68 | + model = openrouter.Gpt35Turbo |
| 69 | + } |
| 70 | + |
| 71 | + promptTokens, err := GetTokenSize(model, question) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + maxTokens := 4097 - promptTokens |
| 77 | + |
| 78 | + respStream, err := client.CreateChatCompletionStream( |
| 79 | + ctx, |
| 80 | + &openrouter.ChatCompletionRequest{ |
| 81 | + Model: p.subType, |
| 82 | + Messages: []openrouter.ChatCompletionMessage{ |
| 83 | + { |
| 84 | + Role: openrouter.ChatMessageRoleSystem, |
| 85 | + Content: "You are a helpful assistant.", |
| 86 | + }, |
| 87 | + { |
| 88 | + Role: openrouter.ChatMessageRoleUser, |
| 89 | + Content: question, |
| 90 | + }, |
| 91 | + }, |
| 92 | + Stream: false, |
| 93 | + Temperature: nil, |
| 94 | + TopP: nil, |
| 95 | + MaxTokens: maxTokens, |
| 96 | + }, |
| 97 | + ) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + defer respStream.Close() |
| 102 | + |
| 103 | + isLeadingReturn := true |
| 104 | + for { |
| 105 | + completion, streamErr := respStream.Recv() |
| 106 | + if streamErr != nil { |
| 107 | + if streamErr == io.EOF { |
| 108 | + break |
| 109 | + } |
| 110 | + return streamErr |
| 111 | + } |
| 112 | + |
| 113 | + data := completion.Choices[0].Message.Content |
| 114 | + if isLeadingReturn && len(data) != 0 { |
| 115 | + if strings.Count(data, "\n") == len(data) { |
| 116 | + continue |
| 117 | + } else { |
| 118 | + isLeadingReturn = false |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if _, err = fmt.Fprintf(writer, "event: message\ndata: %s\n\n", data); err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + flusher.Flush() |
| 126 | + builder.WriteString(data) |
| 127 | + } |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
0 commit comments