generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add acl for openai embedding * feat: upgrade golang.org/x/crypto for security * feat: upgrade golang.org/x/net for security
- Loading branch information
1 parent
ee906b3
commit 1f6028a
Showing
8 changed files
with
1,426 additions
and
1 deletion.
There are no files selected for viewing
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
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,206 @@ | ||
/* | ||
* Copyright 2024 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log" | ||
|
||
"github.com/cloudwego/eino-ext/components/model/gemini" | ||
"github.com/cloudwego/eino/components/model" | ||
"github.com/cloudwego/eino/schema" | ||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/google/generative-ai-go/genai" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
func main() { | ||
// 初始化 Gemini 客户端 | ||
ctx := context.Background() | ||
client, err := genai.NewClient(ctx, option.WithAPIKey("api-key")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer func() { | ||
err = client.Close() | ||
if err != nil { | ||
log.Printf("close client error: %v", err) | ||
} | ||
}() | ||
|
||
// 创建 Gemini 模型 | ||
cm, err := gemini.NewChatModel(ctx, &gemini.Config{ | ||
Client: client, | ||
Model: "gemini-pro", | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// 基本对话示例 | ||
fmt.Println("\n=== Basic Chat ===") | ||
basicChat(ctx, cm) | ||
|
||
// 流式对话示例 | ||
fmt.Println("\n=== Streaming Chat ===") | ||
streamingChat(ctx, cm) | ||
|
||
// 函数调用示例 | ||
fmt.Println("\n=== Function Calling ===") | ||
functionCalling(ctx, cm) | ||
|
||
// 图片处理示例 | ||
fmt.Println("\n=== Image Processing ===") | ||
imageProcessing(ctx, client) | ||
} | ||
|
||
func basicChat(ctx context.Context, cm model.ChatModel) { | ||
resp, err := cm.Generate(ctx, []*schema.Message{ | ||
{ | ||
Role: schema.User, | ||
Content: "What is the capital of France?", | ||
}, | ||
}) | ||
if err != nil { | ||
log.Printf("Generate error: %v", err) | ||
return | ||
} | ||
fmt.Printf("Assistant: %s\n", resp.Content) | ||
} | ||
|
||
func streamingChat(ctx context.Context, cm model.ChatModel) { | ||
stream, err := cm.Stream(ctx, []*schema.Message{ | ||
{ | ||
Role: schema.User, | ||
Content: "Write a short poem about spring.", | ||
}, | ||
}) | ||
if err != nil { | ||
log.Printf("Stream error: %v", err) | ||
return | ||
} | ||
|
||
fmt.Print("Assistant: ") | ||
for { | ||
resp, err := stream.Recv() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
log.Printf("Stream receive error: %v", err) | ||
return | ||
} | ||
fmt.Print(resp.Content) | ||
} | ||
fmt.Println() | ||
} | ||
|
||
func functionCalling(ctx context.Context, cm model.ChatModel) { | ||
err := cm.BindTools([]*schema.ToolInfo{ | ||
{ | ||
Name: "get_weather", | ||
Desc: "Get current weather information for a city", | ||
ParamsOneOf: schema.NewParamsOneOfByOpenAPIV3( | ||
&openapi3.Schema{ | ||
Type: "object", | ||
Properties: map[string]*openapi3.SchemaRef{ | ||
"city": { | ||
Value: &openapi3.Schema{ | ||
Type: "string", | ||
Description: "The city name", | ||
}, | ||
}, | ||
}, | ||
Required: []string{"city"}, | ||
}), | ||
}, | ||
}) | ||
if err != nil { | ||
log.Printf("Bind tools error: %v", err) | ||
return | ||
} | ||
|
||
resp, err := cm.Generate(ctx, []*schema.Message{ | ||
{ | ||
Role: schema.User, | ||
Content: "What's the weather like in Paris today?", | ||
}, | ||
}) | ||
if err != nil { | ||
log.Printf("Generate error: %v", err) | ||
return | ||
} | ||
|
||
if len(resp.ToolCalls) > 0 { | ||
fmt.Printf("Function called: %s\n", resp.ToolCalls[0].Function.Name) | ||
fmt.Printf("Arguments: %s\n", resp.ToolCalls[0].Function.Arguments) | ||
} else { | ||
log.Printf("Function called without tool calls: %s\n", resp.Content) | ||
} | ||
} | ||
|
||
func imageProcessing(ctx context.Context, client *genai.Client) { | ||
file, err := client.UploadFileFromPath(ctx, "examples/test.jpg", &genai.UploadFileOptions{ | ||
DisplayName: "test", | ||
MIMEType: "image/jpeg", | ||
}) | ||
if err != nil { | ||
log.Printf("Upload file error: %v", err) | ||
return | ||
} | ||
defer func() { | ||
err = client.DeleteFile(ctx, file.Name) | ||
if err != nil { | ||
log.Printf("Delete file error: %v", err) | ||
} | ||
}() | ||
|
||
cm, err := gemini.NewChatModel(ctx, &gemini.Config{ | ||
Client: client, | ||
Model: "gemini-1.5-flash", | ||
}) | ||
if err != nil { | ||
log.Printf("NewChatModel error: %v", err) | ||
return | ||
} | ||
|
||
resp, err := cm.Generate(ctx, []*schema.Message{ | ||
{ | ||
Role: schema.User, | ||
MultiContent: []schema.ChatMessagePart{ | ||
{ | ||
Type: schema.ChatMessagePartTypeText, | ||
Text: "What do you see in this image?", | ||
}, | ||
{ | ||
Type: schema.ChatMessagePartTypeImageURL, | ||
ImageURL: &schema.ChatMessageImageURL{ | ||
URI: file.URI, | ||
MIMEType: "image/jpeg", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
log.Printf("Generate error: %v", err) | ||
return | ||
} | ||
fmt.Printf("Assistant: %s\n", resp.Content) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.