-
Notifications
You must be signed in to change notification settings - Fork 27
feat: add dashscope as llm #102
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| Copyright 2023 KubeAGI. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
|
|
||
| "github.com/kubeagi/arcadia/pkg/llms" | ||
| "github.com/kubeagi/arcadia/pkg/llms/dashscope" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| const ( | ||
| samplePrompt = "how to change a deployment's image?" | ||
| ) | ||
|
|
||
| func main() { | ||
| if len(os.Args) == 1 { | ||
| panic("api key is empty") | ||
| } | ||
| apiKey := os.Args[1] | ||
| klog.Infof("sample chat start...\nwe use same prompt: %s to test\n", samplePrompt) | ||
| for _, model := range []dashscope.Model{dashscope.QWEN14BChat, dashscope.QWEN7BChat} { | ||
| klog.V(0).Infof("\nChat with %s\n", model) | ||
| resp, err := sampleChat(apiKey, model) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| klog.V(0).Infof("Response: \n %s\n", resp) | ||
| klog.V(0).Infoln("\nChat again with sse enable") | ||
| err = sampleSSEChat(apiKey, model) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
| klog.Infoln("sample chat done") | ||
| } | ||
|
|
||
| func sampleChat(apiKey string, model dashscope.Model) (llms.Response, error) { | ||
| client := dashscope.NewDashScope(apiKey, false) | ||
| params := dashscope.DefaultModelParams() | ||
| params.Model = model | ||
| params.Input.Messages = []dashscope.Message{ | ||
| {Role: dashscope.System, Content: "You are a kubernetes expert."}, | ||
| {Role: dashscope.User, Content: samplePrompt}, | ||
| } | ||
| return client.Call(params.Marshal()) | ||
| } | ||
|
|
||
| func sampleSSEChat(apiKey string, model dashscope.Model) error { | ||
| client := dashscope.NewDashScope(apiKey, true) | ||
| params := dashscope.DefaultModelParams() | ||
| params.Model = model | ||
| params.Input.Messages = []dashscope.Message{ | ||
| {Role: dashscope.System, Content: "You are a kubernetes expert."}, | ||
| {Role: dashscope.User, Content: samplePrompt}, | ||
| } | ||
| // you can define a customized `handler` on `Event` | ||
| err := client.StreamCall(context.TODO(), params.Marshal(), nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| Copyright 2023 KubeAGI. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package dashscope | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "github.com/kubeagi/arcadia/pkg/llms" | ||
| ) | ||
|
|
||
| const ( | ||
| DashScopeChatURL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation" | ||
| ) | ||
|
|
||
| type Model string | ||
|
|
||
| const ( | ||
| QWEN14BChat Model = "qwen-14b-chat" | ||
| QWEN7BChat Model = "qwen-7b-chat" | ||
| ) | ||
|
|
||
| var _ llms.LLM = (*DashScope)(nil) | ||
|
|
||
| type DashScope struct { | ||
| apiKey string | ||
| sse bool | ||
| } | ||
|
|
||
| func NewDashScope(apiKey string, sse bool) *DashScope { | ||
| return &DashScope{ | ||
| apiKey: apiKey, | ||
| sse: sse, | ||
| } | ||
| } | ||
|
|
||
| func (z DashScope) Type() llms.LLMType { | ||
| return llms.DashScope | ||
| } | ||
|
|
||
| // Call wraps a common AI api call | ||
| func (z *DashScope) Call(data []byte) (llms.Response, error) { | ||
| params := ModelParams{} | ||
| if err := params.Unmarshal(data); err != nil { | ||
| return nil, err | ||
| } | ||
| return do(context.TODO(), DashScopeChatURL, z.apiKey, data, z.sse) | ||
| } | ||
|
|
||
| func (z *DashScope) Validate() (llms.Response, error) { | ||
| return nil, errors.New("not implemented") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| Copyright 2023 KubeAGI. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package dashscope | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| ) | ||
|
|
||
| func setHeaders(req *http.Request, token string, sse bool) { | ||
| if sse { | ||
| // req.Header.Set("Content-Type", "text/event-stream") // Although the documentation says we should do this, but will return a 400 error and the python sdk doesn't do this. | ||
| req.Header.Set("Content-Type", "application/json") | ||
| req.Header.Set("Accept", "text/event-stream") | ||
| req.Header.Set("X-DashScope-SSE", "enable") | ||
| } else { | ||
| req.Header.Set("Content-Type", "application/json") | ||
| req.Header.Set("Accept", "*/*") | ||
| } | ||
| req.Header.Set("Authorization", "Bearer "+token) | ||
| } | ||
|
|
||
| func parseHTTPResponse(resp *http.Response) (data *Response, err error) { | ||
| if err = json.NewDecoder(resp.Body).Decode(&data); err != nil { | ||
| return nil, err | ||
| } | ||
| return data, nil | ||
| } | ||
|
|
||
| func req(ctx context.Context, apiURL, token string, data []byte, sse bool) (*http.Response, error) { | ||
| req, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(data)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| setHeaders(req, token, sse) | ||
|
|
||
| return http.DefaultClient.Do(req) | ||
| } | ||
| func do(ctx context.Context, apiURL, token string, data []byte, sse bool) (*Response, error) { | ||
| resp, err := req(ctx, apiURL, token, data, sse) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer resp.Body.Close() | ||
| return parseHTTPResponse(resp) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| Copyright 2023 KubeAGI. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package dashscope | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
|
|
||
| "github.com/kubeagi/arcadia/pkg/llms" | ||
| ) | ||
|
|
||
| type Role string | ||
|
|
||
| const ( | ||
| System Role = "system" | ||
| User Role = "user" | ||
| Assistant Role = "assistant" | ||
| ) | ||
|
|
||
| var _ llms.ModelParams = (*ModelParams)(nil) | ||
|
|
||
| // +kubebuilder:object:generate=true | ||
|
|
||
| // ModelParams | ||
| // ref: https://help.aliyun.com/zh/dashscope/developer-reference/tongyi-qianwen-7b-14b-api-detailes#25745d61fbx49 | ||
| // do not use 'input.history', according to the above document, this parameter will be deprecated soon. | ||
| // use 'message' in 'parameters.result_format' to keep better compatibility. | ||
| type ModelParams struct { | ||
| Model Model `json:"model"` | ||
| Input Input `json:"input"` | ||
| Parameters Parameters `json:"parameters"` | ||
| } | ||
|
|
||
| // +kubebuilder:object:generate=true | ||
|
|
||
| type Input struct { | ||
| Messages []Message `json:"messages"` | ||
| } | ||
|
|
||
| type Parameters struct { | ||
| TopP float32 `json:"top_p,omitempty"` | ||
| TopK int `json:"top_k,omitempty"` | ||
| Seed int `json:"seed,omitempty"` | ||
| ResultFormat string `json:"result_format,omitempty"` | ||
| } | ||
|
|
||
| // +kubebuilder:object:generate=true | ||
|
|
||
| type Message struct { | ||
| Role Role `json:"role,omitempty"` | ||
| Content string `json:"content,omitempty"` | ||
| } | ||
|
|
||
| func DefaultModelParams() ModelParams { | ||
| return ModelParams{ | ||
| Model: QWEN14BChat, | ||
| Input: Input{ | ||
| Messages: []Message{}, | ||
| }, | ||
| Parameters: Parameters{ | ||
| TopP: 0.5, | ||
| TopK: 0, | ||
| Seed: 1234, | ||
| ResultFormat: "message", | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (params *ModelParams) Marshal() []byte { | ||
| data, err := json.Marshal(params) | ||
| if err != nil { | ||
| return []byte{} | ||
| } | ||
| return data | ||
| } | ||
|
|
||
| func (params *ModelParams) Unmarshal(bytes []byte) error { | ||
| return json.Unmarshal(bytes, params) | ||
| } | ||
|
|
||
| func ValidateModelParams(params ModelParams) error { | ||
| if params.Parameters.TopP < 0 || params.Parameters.TopP > 1 { | ||
| return errors.New("top_p must be in (0, 1)") | ||
| } | ||
|
|
||
| return nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you pls add this embedding service as well ? https://help.aliyun.com/zh/dashscope/developer-reference/text-embedding-quick-start?spm=a2c4g.11186623.0.0.52f85038Fa45wY
See https://github.com/kubeagi/arcadia/tree/main/pkg/embeddings/zhipuai
You can open another PR if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, I will add embedding from another pr.