Files
langchaingo/examples/openai-function-call-streaming-example/openai_function_call_example.go
T
Dmitry Ng 6317685b67 feat: integrate streaming functionality across LLMs and callbacks
- Updated LLMs and callback interfaces to utilize a new streaming package for handling content chunks.
- Changed function signatures to accept streaming.Chunk instead of byte slices for better type safety and clarity.
- Enhanced examples and tests to demonstrate the new streaming capabilities, ensuring compatibility with existing functionality.
- Added reasoning and tool call handling in streaming responses for improved processing of LLM outputs.
2025-06-23 18:52:06 +03:00

117 lines
3.2 KiB
Go

package main
import (
"context"
"fmt"
"log"
"github.com/vxcontrol/langchaingo/jsonschema"
"github.com/vxcontrol/langchaingo/llms"
"github.com/vxcontrol/langchaingo/llms/openai"
"github.com/vxcontrol/langchaingo/llms/streaming"
)
func main() {
llm, err := openai.New(openai.WithModel("gpt-4o"))
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
resp, err := llm.GenerateContent(ctx,
[]llms.MessageContent{
llms.TextParts(llms.ChatMessageTypeHuman, "What is the weather like in Boston?"),
},
llms.WithStreamingFunc(func(_ context.Context, chunk streaming.Chunk) error {
fmt.Println(chunk.String())
return nil
}),
llms.WithTools(tools))
if err != nil {
log.Fatal(err)
}
choice1 := resp.Choices[0]
if choice1.FuncCall != nil {
fmt.Printf("Function call: %v\n", choice1.FuncCall)
}
}
// 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"]}`),
var tools = []llms.Tool{
{
Type: "function",
Function: &llms.FunctionDefinition{
Name: "getCurrentWeather",
Description: "Get the current weather in a given location",
Parameters: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"rationale": {
Type: jsonschema.String,
Description: "The rationale for choosing this function call with these parameters",
},
"location": {
Type: jsonschema.String,
Description: "The city and state, e.g. San Francisco, CA",
},
"unit": {
Type: jsonschema.String,
Enum: []string{"celsius", "fahrenheit"},
},
},
Required: []string{"rationale", "location"},
},
},
},
{
Type: "function",
Function: &llms.FunctionDefinition{
Name: "getTomorrowWeather",
Description: "Get the predicted weather in a given location",
Parameters: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"rationale": {
Type: jsonschema.String,
Description: "The rationale for choosing this function call with these parameters",
},
"location": {
Type: jsonschema.String,
Description: "The city and state, e.g. San Francisco, CA",
},
"unit": {
Type: jsonschema.String,
Enum: []string{"celsius", "fahrenheit"},
},
},
Required: []string{"rationale", "location"},
},
},
},
{
Type: "function",
Function: &llms.FunctionDefinition{
Name: "getSuggestedPrompts",
Description: "Given the user's input prompt suggest some related prompts",
Parameters: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"rationale": {
Type: jsonschema.String,
Description: "The rationale for choosing this function call with these parameters",
},
"suggestions": {
Type: jsonschema.Array,
Items: &jsonschema.Definition{
Type: jsonschema.String,
Description: "A suggested prompt",
},
},
},
Required: []string{"rationale", "suggestions"},
},
},
},
}