Skip to content

Commit e7f75fd

Browse files
matthew29tangcopybara-github
authored andcommitted
feat: Add Chats module for Go SDK (non-stream only)
PiperOrigin-RevId: 742409449
1 parent c660ae4 commit e7f75fd

5 files changed

Lines changed: 504 additions & 0 deletions

File tree

‎chats.go‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2025 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+
// Chats client.
16+
17+
package genai
18+
19+
import (
20+
"context"
21+
)
22+
23+
// Chats provides util functions for creating a new chat session.
24+
// You don't need to initiate this struct. Create a client instance via NewClient, and
25+
// then access Chats through client.Models field.
26+
type Chats struct {
27+
apiClient *apiClient
28+
}
29+
30+
// Chat represents a single chat session (multi-turn conversation) with the model.
31+
//
32+
// client, _ := genai.NewClient(ctx, &genai.ClientConfig{})
33+
// chat, _ := client.Chats.Create(ctx, "gemini-2.0-flash", nil, nil)
34+
// result, err = chat.SendMessage(ctx, genai.Part{Text: "What is 1 + 2?"})
35+
type Chat struct {
36+
Models
37+
apiClient *apiClient
38+
model string
39+
config *GenerateContentConfig
40+
// History of the chat.
41+
comprehensiveHistory []*Content
42+
}
43+
44+
// Create initializes a new chat session.
45+
func (c *Chats) Create(ctx context.Context, model string, config *GenerateContentConfig, history []*Content) (*Chat, error) {
46+
chat := &Chat{
47+
apiClient: c.apiClient,
48+
model: model,
49+
config: config,
50+
comprehensiveHistory: history,
51+
}
52+
chat.Models.apiClient = c.apiClient
53+
return chat, nil
54+
}
55+
56+
func (c *Chat) recordHistory(ctx context.Context, inputContent *Content, cands []*Candidate) {
57+
c.comprehensiveHistory = append(c.comprehensiveHistory, inputContent)
58+
59+
// By default, use the first candidate for history. The user can modify that if they want.
60+
if len(cands) > 0 {
61+
content := cands[0].Content
62+
if content == nil {
63+
return
64+
}
65+
c.comprehensiveHistory = append(c.comprehensiveHistory, copySanitizedModelContent(content))
66+
}
67+
}
68+
69+
// copySanitizedModelContent creates a (shallow) copy of modelContent with role set to
70+
// model and empty text parts removed.
71+
func copySanitizedModelContent(modelContent *Content) *Content {
72+
newContent := &Content{Role: "model"}
73+
for _, part := range modelContent.Parts {
74+
text := (*part).Text
75+
if len(string(text)) > 0 {
76+
newContent.Parts = append(newContent.Parts, part)
77+
}
78+
}
79+
return newContent
80+
}
81+
82+
// SendMessage sends the conversation history with the additional user's message and returns the model's response.
83+
func (c *Chat) SendMessage(ctx context.Context, parts ...Part) (*GenerateContentResponse, error) {
84+
// Transform Parts to single Content
85+
p := make([]*Part, len(parts))
86+
for i, part := range parts {
87+
p[i] = &part
88+
}
89+
inputContent := &Content{Parts: p, Role: "user"}
90+
91+
// Combine history with input content to send to model
92+
contents := append(c.comprehensiveHistory, inputContent)
93+
94+
// Generate Content
95+
modelOutput, err := c.GenerateContent(ctx, c.model, contents, c.config)
96+
if err != nil {
97+
return nil, err
98+
}
99+
100+
// Record history
101+
c.recordHistory(ctx, inputContent, modelOutput.Candidates)
102+
103+
return modelOutput, err
104+
}

‎chats_test.go‎

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
// Copyright 2025 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+
"fmt"
20+
"log"
21+
"net/http"
22+
"net/http/httptest"
23+
"testing"
24+
25+
"cloud.google.com/go/auth"
26+
)
27+
28+
func TestChatsUnitTest(t *testing.T) {
29+
ctx := context.Background()
30+
t.Run("TestServer", func(t *testing.T) {
31+
t.Parallel()
32+
if isDisabledTest(t) {
33+
t.Skip("Skip: disabled test")
34+
}
35+
// Create a test server
36+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37+
w.WriteHeader(http.StatusOK)
38+
fmt.Fprintln(w, `{
39+
"candidates": [
40+
{
41+
"content": {
42+
"role": "model",
43+
"parts": [
44+
{
45+
"text": "1 + 2 = 3"
46+
}
47+
]
48+
},
49+
"finishReason": "STOP",
50+
"avgLogprobs": -0.6608115907699342
51+
}
52+
]
53+
}
54+
`)
55+
}))
56+
defer ts.Close()
57+
58+
t.Logf("Using test server: %s", ts.URL)
59+
cc := &ClientConfig{
60+
HTTPOptions: HTTPOptions{
61+
BaseURL: ts.URL,
62+
},
63+
HTTPClient: ts.Client(),
64+
Credentials: &auth.Credentials{},
65+
}
66+
ac := &apiClient{clientConfig: cc}
67+
client := &Client{
68+
clientConfig: *cc,
69+
Chats: &Chats{apiClient: ac},
70+
}
71+
72+
// Create a new Chat.
73+
var config *GenerateContentConfig = &GenerateContentConfig{Temperature: Ptr[float32](0.5)}
74+
chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", config, nil)
75+
if err != nil {
76+
log.Fatal(err)
77+
}
78+
79+
part := Part{Text: "What is 1 + 2?"}
80+
81+
result, err := chat.SendMessage(ctx, part)
82+
if err != nil {
83+
log.Fatal(err)
84+
}
85+
if result.Text() == "" {
86+
t.Errorf("Response text should not be empty")
87+
}
88+
})
89+
90+
}
91+
92+
func TestChatsText(t *testing.T) {
93+
if *mode != apiMode {
94+
t.Skip("Skip. This test is only in the API mode")
95+
}
96+
ctx := context.Background()
97+
for _, backend := range backends {
98+
t.Run(backend.name, func(t *testing.T) {
99+
t.Parallel()
100+
if isDisabledTest(t) {
101+
t.Skip("Skip: disabled test")
102+
}
103+
client, err := NewClient(ctx, &ClientConfig{Backend: backend.Backend})
104+
if err != nil {
105+
t.Fatal(err)
106+
}
107+
// Create a new Chat.
108+
var config *GenerateContentConfig = &GenerateContentConfig{Temperature: Ptr[float32](0.5)}
109+
chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", config, nil)
110+
if err != nil {
111+
log.Fatal(err)
112+
}
113+
114+
part := Part{Text: "What is 1 + 2?"}
115+
116+
result, err := chat.SendMessage(ctx, part)
117+
if err != nil {
118+
log.Fatal(err)
119+
}
120+
if result.Text() == "" {
121+
t.Errorf("Response text should not be empty")
122+
}
123+
})
124+
}
125+
}
126+
127+
func TestChatsParts(t *testing.T) {
128+
if *mode != apiMode {
129+
t.Skip("Skip. This test is only in the API mode")
130+
}
131+
ctx := context.Background()
132+
for _, backend := range backends {
133+
t.Run(backend.name, func(t *testing.T) {
134+
t.Parallel()
135+
if isDisabledTest(t) {
136+
t.Skip("Skip: disabled test")
137+
}
138+
client, err := NewClient(ctx, &ClientConfig{Backend: backend.Backend})
139+
if err != nil {
140+
t.Fatal(err)
141+
}
142+
// Create a new Chat.
143+
var config *GenerateContentConfig = &GenerateContentConfig{Temperature: Ptr[float32](0.5)}
144+
chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", config, nil)
145+
if err != nil {
146+
log.Fatal(err)
147+
}
148+
149+
parts := make([]Part, 2)
150+
parts[0] = Part{Text: "What is "}
151+
parts[1] = Part{Text: "1 + 2?"}
152+
153+
// Send chat message.
154+
result, err := chat.SendMessage(ctx, parts...)
155+
if err != nil {
156+
log.Fatal(err)
157+
}
158+
if result.Text() == "" {
159+
t.Errorf("Response text should not be empty")
160+
}
161+
})
162+
}
163+
}
164+
165+
func TestChats2Messages(t *testing.T) {
166+
if *mode != apiMode {
167+
t.Skip("Skip. This test is only in the API mode")
168+
}
169+
ctx := context.Background()
170+
for _, backend := range backends {
171+
t.Run(backend.name, func(t *testing.T) {
172+
t.Parallel()
173+
if isDisabledTest(t) {
174+
t.Skip("Skip: disabled test")
175+
}
176+
client, err := NewClient(ctx, &ClientConfig{Backend: backend.Backend})
177+
if err != nil {
178+
t.Fatal(err)
179+
}
180+
// Create a new Chat.
181+
var config *GenerateContentConfig = &GenerateContentConfig{Temperature: Ptr[float32](0.5)}
182+
chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", config, nil)
183+
if err != nil {
184+
log.Fatal(err)
185+
}
186+
187+
// Send first chat message.
188+
part := Part{Text: "What is 1 + 2?"}
189+
190+
result, err := chat.SendMessage(ctx, part)
191+
if err != nil {
192+
log.Fatal(err)
193+
}
194+
if result.Text() == "" {
195+
t.Errorf("Response text should not be empty")
196+
}
197+
198+
// Send second chat message.
199+
part = Part{Text: "Add 1 to the previous result."}
200+
result, err = chat.SendMessage(ctx, part)
201+
if err != nil {
202+
log.Fatal(err)
203+
}
204+
if result.Text() == "" {
205+
t.Errorf("Response text should not be empty")
206+
}
207+
})
208+
}
209+
}
210+
211+
func TestChatsHistory(t *testing.T) {
212+
if *mode != apiMode {
213+
t.Skip("Skip. This test is only in the API mode")
214+
}
215+
ctx := context.Background()
216+
for _, backend := range backends {
217+
t.Run(backend.name, func(t *testing.T) {
218+
t.Parallel()
219+
if isDisabledTest(t) {
220+
t.Skip("Skip: disabled test")
221+
}
222+
client, err := NewClient(ctx, &ClientConfig{Backend: backend.Backend})
223+
if err != nil {
224+
t.Fatal(err)
225+
}
226+
// Create a new Chat with handwritten history.
227+
var config *GenerateContentConfig = &GenerateContentConfig{Temperature: Ptr[float32](0.5)}
228+
history := []*Content{
229+
&Content{
230+
Role: "user",
231+
Parts: []*Part{
232+
&Part{Text: "What is 1 + 2?"},
233+
},
234+
},
235+
&Content{
236+
Role: "model",
237+
Parts: []*Part{
238+
&Part{Text: "3"},
239+
},
240+
},
241+
}
242+
chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", config, history)
243+
if err != nil {
244+
log.Fatal(err)
245+
}
246+
247+
// Send chat message.
248+
part := Part{Text: "Add 1 to the previous result."}
249+
result, err := chat.SendMessage(ctx, part)
250+
if err != nil {
251+
log.Fatal(err)
252+
}
253+
if result.Text() == "" {
254+
t.Errorf("Response text should not be empty")
255+
}
256+
})
257+
}
258+
}

‎client.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type Client struct {
3535
Live *Live
3636
// Caches provides access to the Caches service.
3737
Caches *Caches
38+
// Chats provides util functions for creating a new chat session.
39+
Chats *Chats
3840
// Operations provides access to long-running operations.
3941
Operations *Operations
4042
}
@@ -225,6 +227,7 @@ func NewClient(ctx context.Context, cc *ClientConfig) (*Client, error) {
225227
Models: &Models{apiClient: ac},
226228
Live: &Live{apiClient: ac},
227229
Caches: &Caches{apiClient: ac},
230+
Chats: &Chats{apiClient: ac},
228231
Operations: &Operations{apiClient: ac},
229232
}
230233
return c, nil

0 commit comments

Comments
 (0)