Files
langchaingo/examples/openai-function-call-streaming-example
Dmitry Ng 064f09155c chore(deps): bump Go toolchain to 1.26.5 and fix vulnerable dependencies
- Raised the `go` directive from 1.24.1 to 1.26.5 to satisfy pgx/v5 and ollama's minimum Go version requirements and to pick up 15 patched standard library CVEs.
- Updated `pgx/v5`, `otel`/`otel/sdk`/`otlptracehttp`, `x/net`, and the AWS `eventstream` protocol package to their latest requested versions.
- Remediated govulncheck-flagged CVEs in `grpc`, `x/text`, `x/crypto`, `antchfx/xpath`, AWS SDK `bedrock*`/`s3` services, `etcd/server`, and `mongo-driver` v1/v2.
- Fixed ollama's `MainGPU` API type change (`int` -> `*int`) in `llms/ollama/options.go` without breaking the public `WithRunnerMainGPU` signature.
2026-08-04 13:57:26 +03:00
..
2024-06-20 23:47:52 -04:00

OpenAI Function Call Streaming Example

Welcome to this exciting example of using OpenAI's function calling feature with streaming in Go! 🎉

What does this example do?

This example demonstrates how to use the LangChain Go library to interact with OpenAI's GPT-4 model, specifically showcasing function calling capabilities and streaming responses. Here's a breakdown of the main features:

  1. OpenAI Model Initialization: The code sets up a connection to the GPT-4 Turbo model.

  2. Function Definitions: Three functions are defined as tools that the AI can potentially use:

    • getCurrentWeather: Get current weather for a location
    • getTomorrowWeather: Get predicted weather for a location
    • getSuggestedPrompts: Generate related prompts based on user input
  3. User Query: The example asks the AI about the weather in Boston.

  4. Streaming Response: As the AI generates its response, the code streams and prints each chunk of the response in real-time.

  5. Function Call Detection: If the AI decides to call a function, the code will detect and display this information.

How it works

  1. The program initializes the OpenAI model and sets up the context.
  2. It sends a user query about the weather in Boston.
  3. As the AI generates its response, each chunk is printed to the console.
  4. If the AI decides to call a function (like getCurrentWeather), this will be detected and displayed.

Why is this cool?

  • Real-time Interaction: You can see the AI's thought process as it generates the response chunk by chunk.
  • Function Calling: This showcases how AI can be integrated with external tools or data sources.
  • Flexible Tools: The example defines multiple tools, demonstrating how you can give the AI various capabilities.

Give it a try and watch as the AI decides whether to call a function or provide a direct response about the weather in Boston! ☀️🌦️