基于langchainJS/Chroma /NodeJS 实现RAG

安装 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);

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值