mirror of
https://github.com/vxcontrol/langchaingo.git
synced 2026-07-25 00:25:24 -04:00
examples: clarify openai-function-call-example (#751)
examples: clearify openai-function-call-example clean up the flow and don't use globals
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user