|
| 1 | +/* |
| 2 | +Copyright 2024 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 | +package googleai |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "reflect" |
| 20 | + |
| 21 | + "github.com/dapr/components-contrib/conversation" |
| 22 | + "github.com/dapr/components-contrib/metadata" |
| 23 | + "github.com/dapr/kit/logger" |
| 24 | + kmeta "github.com/dapr/kit/metadata" |
| 25 | + |
| 26 | + "github.com/tmc/langchaingo/llms" |
| 27 | + "github.com/tmc/langchaingo/llms/googleai" |
| 28 | +) |
| 29 | + |
| 30 | +type GoogleAI struct { |
| 31 | + llm llms.Model |
| 32 | + |
| 33 | + logger logger.Logger |
| 34 | +} |
| 35 | + |
| 36 | +func NewGoogleAI(logger logger.Logger) conversation.Conversation { |
| 37 | + g := &GoogleAI{ |
| 38 | + logger: logger, |
| 39 | + } |
| 40 | + |
| 41 | + return g |
| 42 | +} |
| 43 | + |
| 44 | +const defaultModel = "gemini-1.5-flash" |
| 45 | + |
| 46 | +func (g *GoogleAI) Init(ctx context.Context, meta conversation.Metadata) error { |
| 47 | + md := conversation.LangchainMetadata{} |
| 48 | + err := kmeta.DecodeMetadata(meta.Properties, &md) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + model := defaultModel |
| 54 | + if md.Model != "" { |
| 55 | + model = md.Model |
| 56 | + } |
| 57 | + |
| 58 | + opts := []googleai.Option{ |
| 59 | + googleai.WithAPIKey(md.Key), |
| 60 | + googleai.WithDefaultModel(model), |
| 61 | + } |
| 62 | + llm, err := googleai.New( |
| 63 | + ctx, |
| 64 | + opts..., |
| 65 | + ) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + g.llm = llm |
| 71 | + |
| 72 | + if md.CacheTTL != "" { |
| 73 | + cachedModel, cacheErr := conversation.CacheModel(ctx, md.CacheTTL, g.llm) |
| 74 | + if cacheErr != nil { |
| 75 | + return cacheErr |
| 76 | + } |
| 77 | + |
| 78 | + g.llm = cachedModel |
| 79 | + } |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func (g *GoogleAI) GetComponentMetadata() (metadataInfo metadata.MetadataMap) { |
| 84 | + metadataStruct := conversation.LangchainMetadata{} |
| 85 | + metadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, metadata.ConversationType) |
| 86 | + return |
| 87 | +} |
| 88 | + |
| 89 | +func (g *GoogleAI) Converse(ctx context.Context, r *conversation.ConversationRequest) (res *conversation.ConversationResponse, err error) { |
| 90 | + messages := make([]llms.MessageContent, 0, len(r.Inputs)) |
| 91 | + |
| 92 | + for _, input := range r.Inputs { |
| 93 | + role := conversation.ConvertLangchainRole(input.Role) |
| 94 | + |
| 95 | + messages = append(messages, llms.MessageContent{ |
| 96 | + Role: role, |
| 97 | + Parts: []llms.ContentPart{ |
| 98 | + llms.TextPart(input.Message), |
| 99 | + }, |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + opts := []llms.CallOption{} |
| 104 | + |
| 105 | + if r.Temperature > 0 { |
| 106 | + opts = append(opts, conversation.LangchainTemperature(r.Temperature)) |
| 107 | + } |
| 108 | + |
| 109 | + resp, err := g.llm.GenerateContent(ctx, messages, opts...) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + outputs := make([]conversation.ConversationResult, 0, len(resp.Choices)) |
| 115 | + |
| 116 | + for i := range resp.Choices { |
| 117 | + outputs = append(outputs, conversation.ConversationResult{ |
| 118 | + Result: resp.Choices[i].Content, |
| 119 | + Parameters: r.Parameters, |
| 120 | + }) |
| 121 | + } |
| 122 | + |
| 123 | + res = &conversation.ConversationResponse{ |
| 124 | + Outputs: outputs, |
| 125 | + } |
| 126 | + |
| 127 | + return res, nil |
| 128 | +} |
| 129 | + |
| 130 | +func (g *GoogleAI) Close() error { |
| 131 | + return nil |
| 132 | +} |
0 commit comments