svijayanand commited on
Commit
57191f4
·
verified ·
1 Parent(s): 9eb53e1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ from dotenv import load_dotenv
3
+ from pathlib import Path
4
+ from ingest_data import download_data_and_create_embedding
5
+
6
+ from langchain_community.vectorstores import FAISS
7
+ from langchain_core.runnables.passthrough import RunnablePassthrough
8
+ from langchain_core.output_parsers import StrOutputParser
9
+ from langchain_core.prompts import ChatPromptTemplate
10
+ from langchain_openai import ChatOpenAI
11
+ from ingest_data import underlying_embeddings, openai_api_key
12
+
13
+ from langchain.chat_models import ChatOpenAI
14
+ from langchain.prompts import ChatPromptTemplate
15
+ from langchain.schema import StrOutputParser
16
+
17
+ import chainlit as cl
18
+
19
+ # load env variables
20
+ load_dotenv()
21
+
22
+ # Specify the path to the file you want to check
23
+ file_path = Path('./faiss_index/index.faiss')
24
+
25
+ # Check if the file exists
26
+ if file_path.exists():
27
+ print("Embeddings already done, use the saved index")
28
+ # Combine the retrieved data with the output of the LLM
29
+ vector_store = FAISS.load_local(
30
+ "faiss_index", underlying_embeddings, allow_dangerous_deserialization=True
31
+ )
32
+ else:
33
+ vector_store = download_data_and_create_embedding()
34
+
35
+
36
+ # create a prompt template to send to our LLM that will incorporate the documents from our retriever with the
37
+ # question we ask the chat model
38
+ prompt_template = ChatPromptTemplate.from_template(
39
+ "Answer the {question} based on the following {context}."
40
+ )
41
+
42
+ # create a retriever for our documents
43
+ retriever = vector_store.as_retriever()
44
+
45
+ # create a chat model / LLM
46
+ chat_model = ChatOpenAI(
47
+ model="gpt-4o-2024-05-13", temperature=0, api_key=openai_api_key
48
+ )
49
+
50
+ # create a parser to parse the output of our LLM
51
+ parser = StrOutputParser()
52
+
53
+ # 💻 Create the sequence (recipe)
54
+ runnable_chain = (
55
+ # TODO: How do we chain the output of our retriever, prompt, model and model output parser so that we can get a good answer to our query?
56
+ {"context": retriever, "question": RunnablePassthrough()}
57
+ | prompt_template
58
+ | chat_model
59
+ | StrOutputParser()
60
+ )
61
+
62
+
63
+ # Asynchronous execution (e.g., for a better a chatbot user experience)
64
+ async def call_chain_async(question):
65
+ output_chunks = await runnable_chain.ainvoke(question)
66
+ return output_chunks
67
+
68
+
69
+ # output_stream = asyncio.run(call_chain_async("What are some good sci-fi movies from the 1980s?"))
70
+ # print("".join(output_stream))
71
+
72
+ @cl.on_chat_start
73
+ async def on_chat_start():
74
+ model = ChatOpenAI(streaming=True)
75
+ prompt = ChatPromptTemplate.from_messages(
76
+ [
77
+ (
78
+ "system",
79
+ "You're a very knowledgeable historian who provides accurate and eloquent answers to historical questions.",
80
+ ),
81
+ ("human", "{question}"),
82
+ ]
83
+ )
84
+ runnable = prompt | model | StrOutputParser()
85
+ cl.user_session.set("runnable", runnable)
86
+
87
+
88
+ # @cl.on_message
89
+ # async def on_message(message: cl.Message):
90
+ # runnable = cl.user_session.get("runnable") # type: Runnable
91
+
92
+ # msg = cl.Message(content="")
93
+
94
+ # async for chunk in runnable.astream(
95
+ # {"question": message.content},
96
+ # config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
97
+ # ):
98
+ # await msg.stream_token(chunk)
99
+
100
+ # await msg.send()
101
+
102
+ @cl.on_message
103
+ async def main(question):
104
+ response = await call_chain_async(question.content)
105
+ await cl.Message(content=response).send()