|
| 1 | +/* |
| 2 | +Copyright 2025 The Dapr Authors |
| 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 | + |
| 16 | +package deepseek |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "reflect" |
| 21 | + |
| 22 | + "github.com/dapr/components-contrib/conversation" |
| 23 | + "github.com/dapr/components-contrib/metadata" |
| 24 | + "github.com/dapr/kit/logger" |
| 25 | + kmeta "github.com/dapr/kit/metadata" |
| 26 | + |
| 27 | + deepseek_go "github.com/cohesion-org/deepseek-go" |
| 28 | +) |
| 29 | + |
| 30 | +type Deepseek struct { |
| 31 | + llm *deepseek_go.Client |
| 32 | + md DeepseekMetadata |
| 33 | + |
| 34 | + logger logger.Logger |
| 35 | +} |
| 36 | + |
| 37 | +func NewDeepseek(logger logger.Logger) conversation.Conversation { |
| 38 | + o := &Deepseek{ |
| 39 | + logger: logger, |
| 40 | + } |
| 41 | + |
| 42 | + return o |
| 43 | +} |
| 44 | + |
| 45 | +func (d *Deepseek) Init(ctx context.Context, meta conversation.Metadata) error { |
| 46 | + md := DeepseekMetadata{} |
| 47 | + err := kmeta.DecodeMetadata(meta.Properties, &md) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + d.llm = deepseek_go.NewClient(md.Key) |
| 53 | + d.md = md |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func (d *Deepseek) GetComponentMetadata() (metadataInfo metadata.MetadataMap) { |
| 58 | + metadataStruct := DeepseekMetadata{} |
| 59 | + metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, metadata.ConversationType) |
| 60 | + return |
| 61 | +} |
| 62 | + |
| 63 | +func (d *Deepseek) Converse(ctx context.Context, r *conversation.ConversationRequest) (res *conversation.ConversationResponse, err error) { |
| 64 | + messages := make([]deepseek_go.ChatCompletionMessage, 0, len(r.Inputs)) |
| 65 | + |
| 66 | + for _, input := range r.Inputs { |
| 67 | + messages = append(messages, deepseek_go.ChatCompletionMessage{ |
| 68 | + Role: string(input.Role), |
| 69 | + Content: input.Message, |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + request := &deepseek_go.ChatCompletionRequest{ |
| 74 | + Model: deepseek_go.DeepSeekChat, |
| 75 | + Messages: messages, |
| 76 | + } |
| 77 | + |
| 78 | + if d.md.MaxTokens > 0 { |
| 79 | + request.MaxTokens = d.md.MaxTokens |
| 80 | + } |
| 81 | + |
| 82 | + if r.Temperature > 0 { |
| 83 | + request.Temperature = float32(r.Temperature) |
| 84 | + } |
| 85 | + |
| 86 | + resp, err := d.llm.CreateChatCompletion(ctx, request) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + outputs := make([]conversation.ConversationResult, 0, len(resp.Choices)) |
| 92 | + |
| 93 | + for i := range resp.Choices { |
| 94 | + outputs = append(outputs, conversation.ConversationResult{ |
| 95 | + Result: resp.Choices[i].Message.Content, |
| 96 | + Parameters: r.Parameters, |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + res = &conversation.ConversationResponse{ |
| 101 | + Outputs: outputs, |
| 102 | + } |
| 103 | + |
| 104 | + return res, nil |
| 105 | +} |
| 106 | + |
| 107 | +func (d *Deepseek) Close() error { |
| 108 | + return nil |
| 109 | +} |
0 commit comments