@@ -34,7 +34,7 @@ const (
3434func main () {
3535 fmt .Println ()
3636
37- client , err := llm .NewOpenAIClient (llm .WithBaseURL (localhost ))
37+ client , err := llm .NewClient (llm .WithBaseURL (localhost ))
3838 if err != nil {
3939 log .Fatalf ("failed to create client: %v" , err )
4040 }
@@ -51,7 +51,7 @@ func main() {
5151 if err != nil {
5252 slog .Error ("embedding error" , "err" , err )
5353 } else {
54- slog .Info ("embed success" , "vector" , embedRes .Vector () )
54+ slog .Info ("embed success" , "vector" , embedRes .Vector )
5555 }
5656
5757 embedBatchReq := llm.EmbedBatchRequest {
@@ -63,58 +63,41 @@ func main() {
6363 if err != nil {
6464 slog .Error ("embedding error" , "err" , err )
6565 } else {
66- slog .Info ("embed success" , "vectors" , embedBatchRes .Vectors () )
66+ slog .Info ("embed success" , "vectors" , embedBatchRes .Vectors )
6767 }
6868
6969 // Start a new chat with a system prompt and model
70- chat := client .StartChat ("You are a helpful assistant." , model )
70+ chat , _ := client .NewChat ("You are a helpful assistant." , model )
7171
7272 // Perform a single exchange
7373 resp , err := chat .Send (ctx , "What's the capital of France?" )
7474 if err != nil {
7575 log .Fatalf ("chat send failed: %v" , err )
7676 }
7777
78- // Extract and print all candidate parts
79- for _ , c := range resp .Candidates () {
80- for _ , part := range c .Parts () {
81- if text , ok := part .AsText (); ok {
82- fmt .Println (">>" , text )
83- }
84- }
85- }
78+ fmt .Println (">>" , resp .Content )
8679
8780 stream , err := chat .SendStreaming (ctx , "Write a short poem about the sea." )
8881 if err != nil {
8982 log .Fatalf ("streaming failed: %v" , err )
9083 }
91- var streamedText string
92- var llmError error
84+
85+ var (
86+ streamedText string
87+ llmError error
88+ )
9389
9490 for response , err := range stream {
9591 if err != nil {
9692 slog .Error ("error reading streaming LLM response" )
97- llmError = err
98- break
99- }
100- if response == nil {
101- break
102- }
10393
104- if len (response .Candidates ()) == 0 {
105- slog .Error ("No candidates in response" )
94+ llmError = err
10695
10796 break
10897 }
10998
110- candidate := response .Candidates ()[0 ]
111-
112- for _ , part := range candidate .Parts () {
113- if text , ok := part .AsText (); ok {
114- streamedText += text
115- fmt .Print (text )
116- }
117- }
99+ streamedText += response .Content
100+ fmt .Print (response .Content )
118101 }
119102
120103 if err != nil {
@@ -126,14 +109,7 @@ func main() {
126109 log .Fatalf ("chat send failed: %v" , err )
127110 }
128111
129- // Extract and print all candidate parts
130- for _ , c := range resp .Candidates () {
131- for _ , part := range c .Parts () {
132- if text , ok := part .AsText (); ok {
133- fmt .Println (">>" , text )
134- }
135- }
136- }
112+ fmt .Println (">>" , resp .Content )
137113
138114 fmt .Println () // final newline
139115}
0 commit comments