forked from google/generative-ai-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from zzxwill/response
fix: provide function to convert genai.GenerateContentResponse to string
- Loading branch information
Showing
49 changed files
with
75 additions
and
24,328 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package gemini | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/google/generative-ai-go/genai" | ||
) | ||
|
||
func ResponseToString(resp *genai.GenerateContentResponse) string { | ||
return responseString(resp) | ||
} | ||
|
||
// responseString is copied from https://github.com/google/generative-ai-go/blob/2813ecb3f680192c8742eccfbf44d1eebb659829/genai/client_test.go#L529 | ||
func responseString(resp *genai.GenerateContentResponse) string { | ||
var b strings.Builder | ||
for i, cand := range resp.Candidates { | ||
if len(resp.Candidates) > 1 { | ||
fmt.Fprintf(&b, "%d:", i+1) | ||
} | ||
b.WriteString(contentString(cand.Content)) | ||
} | ||
return b.String() | ||
} | ||
|
||
func contentString(c *genai.Content) string { | ||
var b strings.Builder | ||
if c == nil || c.Parts == nil { | ||
return "" | ||
} | ||
for i, part := range c.Parts { | ||
if i > 0 { | ||
fmt.Fprintf(&b, ";") | ||
} | ||
fmt.Fprintf(&b, "%v", part) | ||
} | ||
return b.String() | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.