mirror of
https://github.com/vxcontrol/langchaingo.git
synced 2026-07-21 08:55:25 -04:00
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/tmc/langchaingo/llms"
|
|
"github.com/tmc/langchaingo/llms/openai"
|
|
"github.com/tmc/langchaingo/schema"
|
|
)
|
|
|
|
func main() {
|
|
llm, err := openai.NewChat(openai.WithModel("gpt-3.5-turbo-0613"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
completion, err := llm.Call(ctx, []schema.ChatMessage{
|
|
schema.HumanChatMessage{Content: "What is the weather like in Boston?"},
|
|
}, llms.WithFunctions(functions))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if completion.FunctionCall != nil {
|
|
fmt.Printf("Function call: %v\n", completion.FunctionCall)
|
|
}
|
|
}
|
|
|
|
func getCurrentWeather(location string, unit string) (string, error) {
|
|
weatherInfo := map[string]interface{}{
|
|
"location": location,
|
|
"temperature": "72",
|
|
"unit": unit,
|
|
"forecast": []string{"sunny", "windy"},
|
|
}
|
|
b, err := json.Marshal(weatherInfo)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
var functions = []llms.FunctionDefinition{
|
|
{
|
|
Name: "getCurrentWeather",
|
|
Description: "Get the current weather in a given location",
|
|
Parameters: json.RawMessage(`{"type": "object", "properties": {"location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}}, "required": ["location"]}`),
|
|
},
|
|
}
|