Created
October 13, 2023 06:13
-
-
Save kentcdodds/57eb495fae47f200b5f1e9278fbe26e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dotenv/config.js' | |
import chromadb from 'chromadb' | |
import { RetrievalQAChain } from 'langchain/chains' | |
import { ChatOpenAI } from 'langchain/chat_models/openai' | |
import { OpenAIEmbeddings } from 'langchain/embeddings/openai' | |
import { Chroma } from 'langchain/vectorstores/chroma' | |
const { OpenAIEmbeddingFunction, ChromaClient } = chromadb | |
const COLLECTION_NAME = 'test-collection' | |
const DB_URL = 'https://epic-chromadb.fly.dev' | |
if (!process.env.CHROMA_SERVER_AUTH_CREDENTIALS) { | |
throw new Error('CHROMA_SERVER_AUTH_CREDENTIALS is not set') | |
} | |
if (!process.env.OPENAI_API_KEY) { | |
throw new Error('OPENAI_API_KEY is not set') | |
} | |
export const client = new ChromaClient({ | |
path: DB_URL, | |
auth: { | |
provider: 'token', | |
credentials: process.env.CHROMA_SERVER_AUTH_CREDENTIALS, | |
}, | |
}) | |
const embedder = new OpenAIEmbeddingFunction({ | |
openai_api_key: process.env.OPENAI_API_KEY, | |
}) | |
await client.deleteCollection({ name: COLLECTION_NAME }) | |
const collection = await client.createCollection({ | |
name: COLLECTION_NAME, | |
embeddingFunction: embedder, | |
}) | |
collection.add({ | |
ids: ['mary', 'spiderman'], | |
documents: [ | |
`Mary had a little lamb, it's fleece was white as snow, and everywhere that Mary went, the lamb was sure to go`, | |
`Spiderman is a superhero who lives in New York City. His real name is Peter Parker and he was bitten by a radioactive spider.`, | |
], | |
metadatas: [{ name: 'mary' }, { name: 'spiderman' }], | |
}) | |
const vectorStore = await Chroma.fromExistingCollection( | |
new OpenAIEmbeddings({ openAIApiKey: process.env.OPENAI_API_KEY }), | |
{ collectionName: COLLECTION_NAME, url: DB_URL, index: client } | |
) | |
const model = new ChatOpenAI({ modelName: 'gpt-3.5-turbo' }) | |
const chain = RetrievalQAChain.fromLLM(model, vectorStore.asRetriever()) | |
const response = await chain.call({ | |
query: `What color was the lamb?`, | |
}) | |
console.log(response) // <-- gives something like: | |
// { text: "I'm sorry, but I don't have enough information to answer that question." } |
I forgot to add an await
to collection.add
🤦♂️
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With verbose mode enabled: