| 1 |
// Copyright 2023 Google LLC |
| 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 genai |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
) |
| 20 |
|
| 21 |
// A ChatSession provides interactive chat. |
| 22 |
type ChatSession struct { |
| 23 |
m *GenerativeModel |
| 24 |
History []*Content |
| 25 |
} |
| 26 |
|
| 27 |
// StartChat starts a chat session. |
| 28 |
func (m *GenerativeModel) StartChat() *ChatSession { |
| 29 |
return &ChatSession{m: m} |
| 30 |
} |
| 31 |
|
| 32 |
// SendMessage sends a request to the model as part of a chat session. |
| 33 |
func (cs *ChatSession) SendMessage(ctx context.Context, parts ...Part) (*GenerateContentResponse, error) { |
| 34 |
// Call the underlying client with the entire history plus the argument Content. |
| 35 |
cs.History = append(cs.History, NewUserContent(parts...)) |
| 36 |
req := cs.m.newGenerateContentRequest(cs.History...) |
| 37 |
cc := int32(1) |
| 38 |
req.GenerationConfig.CandidateCount = &cc |
| 39 |
resp, err := cs.m.generateContent(ctx, req) |
| 40 |
if err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
cs.addToHistory(resp.Candidates) |
| 44 |
return resp, nil |
| 45 |
} |
| 46 |
|
| 47 |
// SendMessageStream is like SendMessage, but with a streaming request. |
| 48 |
func (cs *ChatSession) SendMessageStream(ctx context.Context, parts ...Part) *GenerateContentResponseIterator { |
| 49 |
cs.History = append(cs.History, NewUserContent(parts...)) |
| 50 |
req := cs.m.newGenerateContentRequest(cs.History...) |
| 51 |
var cc int32 = 1 |
| 52 |
req.GenerationConfig.CandidateCount = &cc |
| 53 |
streamClient, err := cs.m.c.pc.StreamGenerateContent(ctx, req) |
| 54 |
return &GenerateContentResponseIterator{ |
| 55 |
sc: streamClient, |
| 56 |
err: err, |
| 57 |
cs: cs, |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// By default, use the first candidate for history. The user can modify that if they want. |
| 62 |
func (cs *ChatSession) addToHistory(cands []*Candidate) { |
| 63 |
if len(cands) > 0 { |
| 64 |
c := cands[0].Content |
| 65 |
if c == nil { |
| 66 |
return |
| 67 |
} |
| 68 |
cs.History = append(cs.History, copySanitizedModelContent(c)) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// copySanitizedModelContent creates a (shallow) copy of c with role set to |
| 73 |
// model and empty text parts removed. |
| 74 |
func copySanitizedModelContent(c *Content) *Content { |
| 75 |
newc := &Content{Role: roleModel} |
| 76 |
for _, part := range c.Parts { |
| 77 |
if t, ok := part.(Text); !ok || len(string(t)) > 0 { |
| 78 |
newc.Parts = append(newc.Parts, part) |
| 79 |
} |
| 80 |
} |
| 81 |
return newc |
| 82 |
} |