File size: 917 Bytes
d638db5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import chainlit as cl
from langchain_openai import ChatOpenAI
from langchain.chains import RetrievalQA
from langchain.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

llm = llm = ChatOpenAI(
    api_key="ollama",
    model='llama3.2',
    base_url="http://localhost:11434/v1",
    temperature=0
)

# Load the persisted Chroma database
persist_directory = 'mydb'
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embeddings)

# Create a retriever from the vector store
retriever = vectordb.as_retriever()

# Set up the QA chain
qa_chain = RetrievalQA.from_chain_type(llm=llm, chain_type='stuff', retriever=retriever)

# Define the Chainlit app
@cl.on_message
def main(message):
    response = qa_chain.run(message.content)
    cl.Message(content=response).send()