puneetm85 commited on
Commit
e5c2dc2
·
1 Parent(s): 9380c2d

changes to physics.app.py

Browse files
Files changed (1) hide show
  1. physics_bot/physics_app.py +15 -8
physics_bot/physics_app.py CHANGED
@@ -5,9 +5,8 @@ from langchain.schema.runnable import Runnable
5
  from langchain.schema.runnable.config import RunnableConfig
6
  import chainlit as cl
7
  from langchain_community.embeddings import OpenAIEmbeddings
8
- from langchain_core.prompts import PromptTemplate
9
  from langchain_community.vectorstores import FAISS
10
-
11
 
12
  @cl.on_chat_start
13
  async def on_chat_start():
@@ -15,9 +14,9 @@ async def on_chat_start():
15
  vectorstore = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
16
  retriever = vectorstore.as_retriever(search_kwargs={"k": 1})
17
 
18
- model = ChatOpenAI(streaming=True)
19
 
20
- prompt = ChatPromptTemplate.from_messages(
21
  [
22
  (
23
  "system",
@@ -33,12 +32,20 @@ async def on_chat_start():
33
  "Example1: “The ball rolled towards the red car.” The red car would be the reference point."
34
  "Example2: “The mall is two miles north of my house.” Your house would be the reference point.",
35
  ),
36
- ("human", "{question}"),
37
  ]
38
  )
39
-
40
- runnable = retriever| prompt | model | StrOutputParser()
41
- cl.user_session.set("runnable", runnable)
 
 
 
 
 
 
 
 
42
 
43
 
44
  @cl.on_message
 
5
  from langchain.schema.runnable.config import RunnableConfig
6
  import chainlit as cl
7
  from langchain_community.embeddings import OpenAIEmbeddings
 
8
  from langchain_community.vectorstores import FAISS
9
+ from langchain_core.runnables.passthrough import RunnablePassthrough
10
 
11
  @cl.on_chat_start
12
  async def on_chat_start():
 
14
  vectorstore = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
15
  retriever = vectorstore.as_retriever(search_kwargs={"k": 1})
16
 
17
+ chat_model = ChatOpenAI(streaming=True, model="gpt-4")
18
 
19
+ prompt_template = ChatPromptTemplate.from_messages(
20
  [
21
  (
22
  "system",
 
32
  "Example1: “The ball rolled towards the red car.” The red car would be the reference point."
33
  "Example2: “The mall is two miles north of my house.” Your house would be the reference point.",
34
  ),
35
+ ("human", "Using this context: {context}, please answer this question: {question}"),
36
  ]
37
  )
38
+ parser = StrOutputParser()
39
+ runnable_chain = (
40
+ {
41
+ "context": retriever,
42
+ "question": RunnablePassthrough(),
43
+ }
44
+ | prompt_template
45
+ | chat_model
46
+ | parser
47
+ )
48
+ cl.user_session.set("runnable", runnable_chain)
49
 
50
 
51
  @cl.on_message