Skip to content

taleye-com/ai-tool-calling-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Tool Calling Examples

A comprehensive, beginner-friendly guide to AI tool calling patterns with runnable examples in JavaScript/TypeScript and Python.

🎯 What You'll Learn

  • Basic Single Tool Calling: How to call one tool at a time
  • Parallel Tool Calling: Execute multiple tools simultaneously for efficiency
  • Sequential Chaining: Chain tool calls where outputs feed into subsequent calls
  • Real-World Applications: Practical examples combining multiple patterns

πŸ“ Repository Structure

ai-tool-calling-examples/
β”œβ”€β”€ javascript/          # JavaScript/TypeScript examples
β”‚   β”œβ”€β”€ 01-basic-single-tool/
β”‚   β”œβ”€β”€ 02-parallel-tools/
β”‚   β”œβ”€β”€ 03-sequential-chaining/
β”‚   └── 04-real-world-examples/
β”‚
└── python/             # Python examples
    β”œβ”€β”€ 01-basic-single-tool/
    β”œβ”€β”€ 02-parallel-tools/
    β”œβ”€β”€ 03-sequential-chaining/
    └── 04-real-world-examples/

πŸš€ Quick Start

Option 1: Run with Mock Data (No API Key Needed)

Perfect for learning the patterns without needing an API key!

JavaScript/TypeScript:

cd javascript
npm install
node 01-basic-single-tool/simple-tool-call.js

Python:

cd python
pip install -r requirements.txt
python 01-basic-single-tool/simple_tool_call.py

Option 2: Run with Real AI (OpenAI or Anthropic)

To use actual AI models for tool calling:

  1. Copy the environment template:

    cp .env.example .env
  2. Add your API key to .env:

    # For OpenAI (get key from https://platform.openai.com/api-keys)
    OPENAI_API_KEY=sk-...
    AI_PROVIDER=openai
    
    # OR for Anthropic (get key from https://console.anthropic.com/settings/keys)
    ANTHROPIC_API_KEY=sk-ant-...
    AI_PROVIDER=anthropic
  3. Run the real AI examples:

    JavaScript:

    cd javascript
    npm install
    node 01-basic-single-tool/with-real-ai.js
    node 02-parallel-tools/with-real-ai.js

    Python:

    cd python
    pip install -r requirements.txt
    python 01-basic-single-tool/with_real_ai.py

Option 3: Run with Local AI (FREE! - Ollama, LM Studio)

Use local open-source models for FREE - no API key needed!

Quick start with Ollama:

# 1. Install Ollama
curl -fsSL https://ollama.com/install.sh | sh

# 2. Pull a model
ollama pull llama3.2:latest

# 3. Configure .env
AI_PROVIDER=openai
OPENAI_BASE_URL=http://localhost:11434/v1
OPENAI_API_KEY=ollama
OPENAI_MODEL=llama3.2:latest

# 4. Run examples!
cd javascript
node 01-basic-single-tool/with-real-ai.js

πŸ“– Full guide: See OPENAI_COMPATIBLE.md for detailed setup with Ollama, LM Studio, Azure OpenAI, and more.

πŸ“š Examples Overview

01 - Basic Single Tool

Learn the fundamentals of tool calling with a simple calculator example.

  • Use Case: Performing basic arithmetic operations
  • Pattern: One tool, one call
  • Best For: Understanding tool definition and execution

02 - Parallel Tools

Call multiple independent tools simultaneously for better performance.

  • Use Case: Getting weather and time from different sources
  • Pattern: Multiple independent tool calls
  • Best For: Optimizing performance when tools don't depend on each other

03 - Sequential Chaining

Chain multiple tool calls where each depends on the previous result.

  • Use Case: User lookup β†’ preferences β†’ personalized recommendations
  • Pattern: Output of one tool feeds into the next
  • Best For: Complex workflows with dependencies

04 - Real-World Examples

Complete applications combining multiple patterns.

  • Web Research Agent: Search, fetch, and summarize web content
  • Data Processing Pipeline: ETL operations with multiple steps
  • Content Analyzer: Multi-step content analysis and reporting

πŸŽ“ Learning Path

  1. Start with 01-basic-single-tool to understand fundamentals
  2. Move to 02-parallel-tools to learn optimization techniques
  3. Progress to 03-sequential-chaining for complex workflows
  4. Explore 04-real-world-examples for practical applications

πŸ”§ Two Ways to Learn

1. Conceptual Examples (Mock AI)

Learn the patterns without API keys using simulated AI responses:

  • simple-tool-call.js/py - Basic concepts
  • concurrent-calls.js/py - Parallel patterns
  • chained-tools.js/py - Sequential patterns

2. Real AI Integration

See how it works with actual AI providers:

  • with-real-ai.js/py - OpenAI or Anthropic integration
  • Real tool calling with GPT-4 or Claude
  • Production-ready code examples

The patterns work with:

  • βœ… OpenAI Function Calling (GPT-4, GPT-3.5)
  • βœ… Anthropic Tool Use (Claude 3.5 Sonnet)
  • βœ… OpenAI-Compatible APIs:
    • Azure OpenAI
    • Ollama (Local - Llama, Mistral, etc.)
    • LM Studio (Local with GUI)
    • LocalAI, vLLM, and more
  • βœ… LangChain Tools
  • βœ… Custom implementations

πŸ’‘ Want to use local AI models? See OPENAI_COMPATIBLE.md for setup with Ollama, LM Studio, and others!

πŸ’‘ Key Concepts

Tool Definition

Tools are functions the AI can call to perform specific actions:

{
  name: "calculator",
  description: "Performs arithmetic operations",
  parameters: {
    operation: "add | subtract | multiply | divide",
    a: "first number",
    b: "second number"
  }
}

Tool Execution Flow

  1. AI receives user request
  2. AI determines which tools to call
  3. Tools execute and return results
  4. AI processes results and responds to user

When to Use Each Pattern

  • Single: Simple, straightforward tasks
  • Parallel: Multiple independent operations
  • Sequential: Complex workflows with dependencies
  • Combined: Real-world applications requiring flexibility

🀝 Contributing

Feel free to add more examples or improve existing ones!

πŸ“„ License

MIT License - feel free to use these examples in your projects.

πŸ”— Resources

About

Comprehensive beginner-friendly guide to AI tool calling patterns with runnable examples in JavaScript and Python. Supports OpenAI, Anthropic, Ollama, and local AI providers.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors