Skip to content

Commit

Permalink
chore(vertexai): consistent usage of context.Background (#3797)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben committed Feb 22, 2024
1 parent 330296f commit 275d117
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
9 changes: 5 additions & 4 deletions vertexai/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var region = "us-central1"
var modelName = "gemini-1.0-pro-vision"

func makeChatRequests(projectId string, region string, modelName string) error {
client, err := genai.NewClient(context.TODO(), projectId, region)
ctx := context.Background()
client, err := genai.NewClient(ctx, projectId, region)
if err != nil {
return fmt.Errorf("error creating client: %v", err)
}
Expand All @@ -38,7 +39,7 @@ func makeChatRequests(projectId string, region string, modelName string) error {
chat := gemini.StartChat()

r, err := chat.SendMessage(
context.TODO(),
ctx,
genai.Text("Hello"))
if err != nil {
return err
Expand All @@ -47,7 +48,7 @@ func makeChatRequests(projectId string, region string, modelName string) error {
fmt.Println(string(rb))

r, err = chat.SendMessage(
context.TODO(),
ctx,
genai.Text("What are all the colors in a rainbow?"))
if err != nil {
return err
Expand All @@ -56,7 +57,7 @@ func makeChatRequests(projectId string, region string, modelName string) error {
fmt.Println(string(rb))

r, err = chat.SendMessage(
context.TODO(),
ctx,
genai.Text("Why does it appear when it rains?"))
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions vertexai/snippets/try_gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ var region = "us-central1"

func tryGemini(w io.Writer, projectId string, region string, modelName string) error {

client, err := genai.NewClient(context.TODO(), projectId, region)
ctx := context.Background()
client, err := genai.NewClient(ctx, projectId, region)
gemini := client.GenerativeModel(modelName)

img := genai.FileData{
MIMEType: "image/jpeg",
FileURI: "gs://generativeai-downloads/images/scones.jpg",
}
prompt := genai.Text("What is in this image?")
resp, err := gemini.GenerateContent(context.Background(), img, prompt)
resp, err := gemini.GenerateContent(ctx, img, prompt)
if err != nil {
return fmt.Errorf("error generating content: %w", err)
}
Expand Down

0 comments on commit 275d117

Please sign in to comment.