xicocdi commited on
Commit
91402bc
1 Parent(s): 34f6bf3

Deploying Pythonic RAG

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  import os
2
  from typing import List
3
  from chainlit.types import AskFileResponse
@@ -25,6 +28,7 @@ Question:
25
  """
26
  user_role_prompt = UserRolePrompt(user_prompt_template)
27
 
 
28
  class RetrievalAugmentedQAPipeline:
29
  def __init__(self, llm: ChatOpenAI(), vector_db_retriever: VectorDatabase) -> None:
30
  self.llm = llm
@@ -39,21 +43,28 @@ class RetrievalAugmentedQAPipeline:
39
 
40
  formatted_system_prompt = system_role_prompt.create_message()
41
 
42
- formatted_user_prompt = user_role_prompt.create_message(question=user_query, context=context_prompt)
 
 
43
 
44
  async def generate_response():
45
- async for chunk in self.llm.astream([formatted_system_prompt, formatted_user_prompt]):
 
 
46
  yield chunk
47
 
48
  return {"response": generate_response(), "context": context_list}
49
 
 
50
  text_splitter = CharacterTextSplitter()
51
 
52
 
53
  def process_text_file(file: AskFileResponse):
54
  import tempfile
55
 
56
- with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as temp_file:
 
 
57
  temp_file_path = temp_file.name
58
 
59
  with open(temp_file_path, "wb") as f:
@@ -93,15 +104,14 @@ async def on_chat_start():
93
  # Create a dict vector store
94
  vector_db = VectorDatabase()
95
  vector_db = await vector_db.abuild_from_list(texts)
96
-
97
  chat_openai = ChatOpenAI()
98
 
99
  # Create a chain
100
  retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
101
- vector_db_retriever=vector_db,
102
- llm=chat_openai
103
  )
104
-
105
  # Let the user know that the system is ready
106
  msg.content = f"Processing `{file.name}` done. You can now ask questions!"
107
  await msg.update()
@@ -119,4 +129,4 @@ async def main(message):
119
  async for stream_resp in result["response"]:
120
  await msg.stream_token(stream_resp)
121
 
122
- await msg.send()
 
1
+ # flake8: noqa
2
+ # pyright: ignore-all
3
+
4
  import os
5
  from typing import List
6
  from chainlit.types import AskFileResponse
 
28
  """
29
  user_role_prompt = UserRolePrompt(user_prompt_template)
30
 
31
+
32
  class RetrievalAugmentedQAPipeline:
33
  def __init__(self, llm: ChatOpenAI(), vector_db_retriever: VectorDatabase) -> None:
34
  self.llm = llm
 
43
 
44
  formatted_system_prompt = system_role_prompt.create_message()
45
 
46
+ formatted_user_prompt = user_role_prompt.create_message(
47
+ question=user_query, context=context_prompt
48
+ )
49
 
50
  async def generate_response():
51
+ async for chunk in self.llm.astream(
52
+ [formatted_system_prompt, formatted_user_prompt]
53
+ ):
54
  yield chunk
55
 
56
  return {"response": generate_response(), "context": context_list}
57
 
58
+
59
  text_splitter = CharacterTextSplitter()
60
 
61
 
62
  def process_text_file(file: AskFileResponse):
63
  import tempfile
64
 
65
+ with tempfile.NamedTemporaryFile(
66
+ mode="w", delete=False, suffix=".txt"
67
+ ) as temp_file:
68
  temp_file_path = temp_file.name
69
 
70
  with open(temp_file_path, "wb") as f:
 
104
  # Create a dict vector store
105
  vector_db = VectorDatabase()
106
  vector_db = await vector_db.abuild_from_list(texts)
107
+
108
  chat_openai = ChatOpenAI()
109
 
110
  # Create a chain
111
  retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
112
+ vector_db_retriever=vector_db, llm=chat_openai
 
113
  )
114
+
115
  # Let the user know that the system is ready
116
  msg.content = f"Processing `{file.name}` done. You can now ask questions!"
117
  await msg.update()
 
129
  async for stream_resp in result["response"]:
130
  await msg.stream_token(stream_resp)
131
 
132
+ await msg.send()