diff --git a/examples/openai-function-call-example/openai_function_call_example.go b/examples/openai-function-call-example/openai_function_call_example.go index 69db40f8..b2f12fa8 100644 --- a/examples/openai-function-call-example/openai_function_call_example.go +++ b/examples/openai-function-call-example/openai_function_call_example.go @@ -24,23 +24,21 @@ func main() { } fmt.Println("Querying for weather in Boston and Chicago..") - resp := queryLLM(ctx, llm, messageHistory) + resp := queryLLM(ctx, llm, messageHistory, availableTools) + fmt.Println("Initial response:", showResponse(resp)) messageHistory = updateMessageHistory(messageHistory, resp) - if resp.Choices[0].Content != "" { - fmt.Println("Response to weather query:", resp.Choices[0].Content) - } - messageHistory = executeToolCalls(ctx, llm, messageHistory, resp) messageHistory = append(messageHistory, llms.TextParts(schema.ChatMessageTypeHuman, "Can you compare the two?")) - fmt.Println("Querying for comparison..") - resp = queryLLM(ctx, llm, messageHistory) + fmt.Println("Querying with tool response...") + resp = queryLLM(ctx, llm, messageHistory, availableTools) fmt.Println(resp.Choices[0].Content) } -// queryLLM queries the LLM with the given message history and returns the response. -func queryLLM(ctx context.Context, llm llms.Model, messageHistory []llms.MessageContent) *llms.ContentResponse { +// queryLLM queries the LLM with the given message history and list of available +// tools, and returns the response. +func queryLLM(ctx context.Context, llm llms.Model, messageHistory []llms.MessageContent, tools []llms.Tool) *llms.ContentResponse { resp, err := llm.GenerateContent(ctx, messageHistory, llms.WithTools(tools)) if err != nil { log.Fatal(err) @@ -48,7 +46,8 @@ func queryLLM(ctx context.Context, llm llms.Model, messageHistory []llms.Message return resp } -// updateMessageHistory updates the message history with the assistant's response. +// updateMessageHistory updates the message history with the assistant's +// response, and translates tool calls. func updateMessageHistory(messageHistory []llms.MessageContent, resp *llms.ContentResponse) []llms.MessageContent { assistantResponse := llms.MessageContent{ Role: schema.ChatMessageTypeAI, @@ -66,7 +65,8 @@ func updateMessageHistory(messageHistory []llms.MessageContent, resp *llms.Conte return append(messageHistory, assistantResponse) } -// executeToolCalls executes the tool calls in the response and returns the updated message history. +// executeToolCalls executes the tool calls in the response and returns the +// updated message history. func executeToolCalls(ctx context.Context, llm llms.Model, messageHistory []llms.MessageContent, resp *llms.ContentResponse) []llms.MessageContent { for _, toolCall := range resp.Choices[0].ToolCalls { switch toolCall.FunctionCall.Name { @@ -95,7 +95,6 @@ func executeToolCalls(ctx context.Context, llm llms.Model, messageHistory []llms }, } messageHistory = append(messageHistory, weatherCallResponse) - fmt.Println("Response to weather query:", args, response) default: log.Fatalf("Unsupported tool: %s", toolCall.FunctionCall.Name) } @@ -105,6 +104,11 @@ func executeToolCalls(ctx context.Context, llm llms.Model, messageHistory []llms } func getCurrentWeather(location string, unit string) (string, error) { + weatherResponses := map[string]string{ + "boston": "72 and sunny", + "chicago": "65 and windy", + } + weatherInfo, ok := weatherResponses[strings.ToLower(location)] if !ok { return "", fmt.Errorf("no weather info for %q", location) @@ -118,12 +122,9 @@ func getCurrentWeather(location string, unit string) (string, error) { return string(b), nil } -var weatherResponses = map[string]string{ - "boston": "72 and sunny", - "chicago": "65 and windy", -} - -var tools = []llms.Tool{ +// availableTools simulates the tools/functions we're making available for +// the model. +var availableTools = []llms.Tool{ { Type: "function", Function: &llms.FunctionDefinition{ @@ -146,3 +147,11 @@ var tools = []llms.Tool{ }, }, } + +func showResponse(resp *llms.ContentResponse) string { + b, err := json.MarshalIndent(resp, "", " ") + if err != nil { + log.Fatal(err) + } + return string(b) +}