Guide
Examples

Examples

To run the included examples, clone this repo (opens in a new tab), run pnpm install, set up your .env file, and then run an example file using tsx.

Environment variables required to run the examples:

  • OPENAI_API_KEY - OpenAI API key
  • PINECONE_API_KEY - Pinecone API key
  • PINECONE_BASE_URL - Pinecone index's base URL
    • You should be able to use a free-tier "starter" index for most of the examples, but you'll need to upgrade to a paid index to run the any of the hybrid search examples
    • Note that Pinecone's free starter index doesn't support namespaces, deleteAll, or hybrid search :sigh:
  • SPLADE_SERVICE_URL - optional; only used for the chatbot hybrid search example

Basic

npx tsx examples/basic.ts

source (opens in a new tab)

Caching

npx tsx examples/caching.ts

source (opens in a new tab)

Redis Caching

This example requires a valid REDIS_URL env var.

npx tsx examples/caching-redis.ts

source (opens in a new tab)

AI Function

This example shows how to use createAIFunction to handle function and tool_calls with the OpenAI chat completions API and Zod.

npx tsx examples/ai-function.ts

source (opens in a new tab)

AI Runner

This example shows how to use createAIRunner to easily invoke a chain of OpenAI chat completion calls, resolving tool / function calls, retrying when necessary, and optionally validating the resulting output via Zod.

Note that createAIRunner takes in a functions array of AIFunction objects created by createAIFunction, as the two utility functions are meant to used together.

npx tsx examples/ai-runner.ts

source (opens in a new tab)

Chatbot

This is a more involved example of a chatbot using RAG. It indexes 100 transcript chunks from the Huberman Lab Podcast (opens in a new tab) into a hybrid Pinecone datastore (opens in a new tab) using OpenAI ada-002 embeddings (opens in a new tab) for the dense vectors and a HuggingFace SPLADE model (opens in a new tab) for the sparse embeddings.

You'll need the following environment variables to run this example:

  • OPENAI_API_KEY
  • PINECONE_API_KEY
  • PINECONE_BASE_URL
    • Note: Pinecone's free starter indexes don't seem to support namespaces or hybrid search, so unfortunately you'll need to upgrade to a paid plan to run this example. See Pinecone's hybrid docs (opens in a new tab) for details on setting up a hybrid index, and make sure it is using the dotproduct metric.
  • SPLADE_SERVICE_URL
npx tsx examples/chatbot/ingest.ts
npx tsx examples/chatbot/cli.ts

source (opens in a new tab)