-
Notifications
You must be signed in to change notification settings - Fork 3
/
chainlit_chat_db.py
46 lines (35 loc) · 1.22 KB
/
chainlit_chat_db.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import chainlit as cl
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain.prompts import PromptTemplate
from langchain.schema import HumanMessage
from langchain_community.vectorstores import Chroma
embeddings = OpenAIEmbeddings(
model="text-embedding-ada-002"
)
chat = ChatOpenAI(model="gpt-3.5-turbo")
prompt = PromptTemplate(template="""文章を元に質問に答えてください。
文章:
{document}
質問: {query}
""", input_variables=["document", "query"])
database = Chroma(
persist_directory="./.data",
embedding_function=embeddings
)
@cl.on_chat_start
async def on_chat_start():
await cl.Message(content="準備ができました!メッセージを入力してください!").send()
@cl.on_message
async def on_message(input_message: cl.Message):
print("入力されたメッセージ: " + input_message.content)
documents = database.similarity_search(input_message.content)
documents_string = ""
for document in documents:
documents_string += f"""
-------------------------
{document.page_content}
"""
result = chat([
HumanMessage(content=prompt.format(document=documents_string, query=input_message.content))
])
await cl.Message(content=result.content).send()