安装 chroma
在anaconfa Powershell Prompt 下安装chroma
pip install chromadb -i https://pypi.tuna.tsinghua.edu.cn/simple
启动Chroma
chroma run --host localhost --port 8000 --path ./chroma_store
RAG 代码
import { Chroma } from "@langchain/community/vectorstores/chroma";
import { OpenAI } from "@langchain/openai";
import { OpenAIEmbeddings } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";
import { formatDocumentsAsString } from "langchain/util/document";
import { TextLoader } from "langchain/document_loaders/fs/text";
import {
RunnableSequence,
RunnablePassthrough,
} from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
process.env['OPENAI_API_KEY']="sk-xxxxxxxxxxxxxxxxxxxxxxx";
process.env['OPENAI_BASE_URL']="https://api.chatanywhere.tech/v1"
// Create docs with a loader
const loader = new TextLoader("./documents/opcua.txt");
const docs = await loader.load();
const openai = new OpenAI({
apiKey: "sk-FfhkMFdQQwDqAR5Mta2UxsU9amU6AoIwDG1NbqqAWGzMpTyi",
baseURL:"https://api.chatanywhere.tech/v1",
model: "gpt-3.5-turbo",
temperature: 0
});
// Create vector store and index the docs
const vectorStore = await Chroma.fromDocuments(docs, new OpenAIEmbeddings(), {
collectionName: "a-test-collection",
}
);
const retriever = vectorStore.asRetriever();
const prompt =
PromptTemplate.fromTemplate(`Answer the question based only on the following context:
{context}
Question: {question}`);
const chain = RunnableSequence.from([
{
context: retriever.pipe(formatDocumentsAsString),
question: new RunnablePassthrough(),
},
prompt,
openai,
new StringOutputParser(),
]);
const result = await chain.invoke("when The first specification of OPC UA was published?");
console.log(result);