jfeng1115 commited on
Commit
39600b6
β€’
1 Parent(s): 17c96aa

fix errors

Browse files
.chainlit/config.toml ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ # Whether to enable telemetry (default: true). No personal data is collected.
3
+ enable_telemetry = true
4
+
5
+ # List of environment variables to be provided by each user to use the app.
6
+ user_env = []
7
+
8
+ # Duration (in seconds) during which the session is saved when the connection is lost
9
+ session_timeout = 3600
10
+
11
+ # Enable third parties caching (e.g LangChain cache)
12
+ cache = false
13
+
14
+ # Follow symlink for asset mount (see https://github.com/Chainlit/chainlit/issues/317)
15
+ # follow_symlink = false
16
+
17
+ [features]
18
+ # Show the prompt playground
19
+ prompt_playground = true
20
+
21
+ [UI]
22
+ # Name of the app and chatbot.
23
+ name = "Chatbot"
24
+
25
+ # Description of the app and chatbot. This is used for HTML tags.
26
+ # description = ""
27
+
28
+ # Large size content are by default collapsed for a cleaner ui
29
+ default_collapse_content = true
30
+
31
+ # The default value for the expand messages settings.
32
+ default_expand_messages = false
33
+
34
+ # Hide the chain of thought details from the user in the UI.
35
+ hide_cot = false
36
+
37
+ # Link to your github repo. This will add a github button in the UI's header.
38
+ # github = ""
39
+
40
+ # Override default MUI light theme. (Check theme.ts)
41
+ [UI.theme.light]
42
+ #background = "#FAFAFA"
43
+ #paper = "#FFFFFF"
44
+
45
+ [UI.theme.light.primary]
46
+ #main = "#F80061"
47
+ #dark = "#980039"
48
+ #light = "#FFE7EB"
49
+
50
+ # Override default MUI dark theme. (Check theme.ts)
51
+ [UI.theme.dark]
52
+ #background = "#FAFAFA"
53
+ #paper = "#FFFFFF"
54
+
55
+ [UI.theme.dark.primary]
56
+ #main = "#F80061"
57
+ #dark = "#980039"
58
+ #light = "#FFE7EB"
59
+
60
+
61
+ [meta]
62
+ generated_by = "0.7.0"
aimakerspace/openai_utils/.chainlit/config.toml ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ # Whether to enable telemetry (default: true). No personal data is collected.
3
+ enable_telemetry = true
4
+
5
+ # List of environment variables to be provided by each user to use the app.
6
+ user_env = []
7
+
8
+ # Duration (in seconds) during which the session is saved when the connection is lost
9
+ session_timeout = 3600
10
+
11
+ # Enable third parties caching (e.g LangChain cache)
12
+ cache = false
13
+
14
+ # Follow symlink for asset mount (see https://github.com/Chainlit/chainlit/issues/317)
15
+ # follow_symlink = false
16
+
17
+ [features]
18
+ # Show the prompt playground
19
+ prompt_playground = true
20
+
21
+ [UI]
22
+ # Name of the app and chatbot.
23
+ name = "Chatbot"
24
+
25
+ # Description of the app and chatbot. This is used for HTML tags.
26
+ # description = ""
27
+
28
+ # Large size content are by default collapsed for a cleaner ui
29
+ default_collapse_content = true
30
+
31
+ # The default value for the expand messages settings.
32
+ default_expand_messages = false
33
+
34
+ # Hide the chain of thought details from the user in the UI.
35
+ hide_cot = false
36
+
37
+ # Link to your github repo. This will add a github button in the UI's header.
38
+ # github = ""
39
+
40
+ # Override default MUI light theme. (Check theme.ts)
41
+ [UI.theme.light]
42
+ #background = "#FAFAFA"
43
+ #paper = "#FFFFFF"
44
+
45
+ [UI.theme.light.primary]
46
+ #main = "#F80061"
47
+ #dark = "#980039"
48
+ #light = "#FFE7EB"
49
+
50
+ # Override default MUI dark theme. (Check theme.ts)
51
+ [UI.theme.dark]
52
+ #background = "#FAFAFA"
53
+ #paper = "#FFFFFF"
54
+
55
+ [UI.theme.dark.primary]
56
+ #main = "#F80061"
57
+ #dark = "#980039"
58
+ #light = "#FFE7EB"
59
+
60
+
61
+ [meta]
62
+ generated_by = "0.7.0"
aimakerspace/openai_utils/chatmodel.py CHANGED
@@ -1,10 +1,10 @@
1
  import openai
2
  from dotenv import load_dotenv
3
  import os
 
4
 
5
  load_dotenv()
6
 
7
-
8
  class ChatOpenAI:
9
  def __init__(self, model_name: str = "gpt-3.5-turbo"):
10
  self.model_name = model_name
@@ -26,9 +26,10 @@ class ChatOpenAI:
26
 
27
  return response
28
 
29
- def run_stream(self, messages, settings, chainlit_msg, text_only: bool = True):
30
- async for stream_resp in await openai.Completion.acreate(
31
- model=self.model_name, prompt=messages, stream=True, **settings
 
32
  ):
33
- token = stream_resp.get("choices")[0].get("text")
34
- await chainlit_msg.stream_token(token)
 
1
  import openai
2
  from dotenv import load_dotenv
3
  import os
4
+ import chainlit as cl
5
 
6
  load_dotenv()
7
 
 
8
  class ChatOpenAI:
9
  def __init__(self, model_name: str = "gpt-3.5-turbo"):
10
  self.model_name = model_name
 
26
 
27
  return response
28
 
29
+ async def stream_with_cl_message(self, message_history, chainlit_msg: cl.Message, text_only: bool = True, settings: dict = {}):
30
+ print("streaming with cl message", message_history);
31
+ async for stream_resp in await openai.ChatCompletion.acreate(
32
+ model=self.model_name, messages=message_history, stream=True, **settings
33
  ):
34
+ token = stream_resp.choices[0]["delta"].get("content", "")
35
+ await chainlit_msg.stream_token(token)
app.py CHANGED
@@ -1,9 +1,9 @@
1
  from dotenv import load_dotenv
2
- import openai #importing openai for API usage
3
- import chainlit as cl #importing chainlit for our app
4
  from aimakerspace.vectordatabase import VectorDatabase
5
  from aimakerspace.vectordatabase import asyncio
6
- from aimakerspace.text_utils import TextFileLoader
7
  import os
8
  import openai
9
  from getpass import getpass
@@ -16,10 +16,9 @@ from aimakerspace.openai_utils.chatmodel import ChatOpenAI
16
 
17
 
18
  load_dotenv()
19
-
20
  openai.api_key = os.environ["OPENAI_API_KEY"]
21
 
22
- openai.api_key = os.environ["OPENAI_API_KEY"]
23
  def load(filename):
24
  text_loader = TextFileLoader(filename)
25
  documents = text_loader.load_documents()
@@ -27,12 +26,15 @@ def load(filename):
27
 
28
  model_name = "gpt-4"
29
 
30
- filname = "data/KingLear.txt"
31
 
32
  vector_db = VectorDatabase()
33
- split_documents = load(filname)
 
 
34
  vector_db = asyncio.run(vector_db.abuild_from_list(split_documents))
35
- chat_openai = ChatOpenAI()
 
36
  user_prompt_template = "{content}"
37
  user_role_prompt = UserRolePrompt(user_prompt_template)
38
  system_prompt_template = (
@@ -76,7 +78,7 @@ class RetrievalAugmentedQAPipeline:
76
 
77
  return self.llm.run([formatted_system_prompt, formatted_user_prompt])
78
 
79
- def stream_pipeline(self, user_query: str, msg: cl.Message) -> str:
80
  context_list = self.vector_db_retriever.search_by_text(user_query, k=4)
81
 
82
  context_prompt = ""
@@ -87,7 +89,10 @@ class RetrievalAugmentedQAPipeline:
87
 
88
  formatted_user_prompt = user_prompt.create_message(user_query=user_query)
89
 
90
- self.llm.stream([formatted_system_prompt, formatted_user_prompt])
 
 
 
91
 
92
 
93
  @cl.on_chat_start # marks a function that will be executed at the start of a user session
@@ -108,11 +113,12 @@ def start_chat():
108
 
109
  @cl.on_message # this function will be called every time a user inputs a message in the UI
110
  async def main(message: str):
 
111
 
112
- qaPipeline = RetrievalAugmentedQAPipeline(vector_db_retriever=vector_db, llm=chat_openai)
113
  msg = cl.Message(content="")
114
 
115
- qaPipeline.stream_pipeline(user_query=message, msg=msg)
116
  await msg.send()
117
 
118
 
 
1
  from dotenv import load_dotenv
2
+ import openai
3
+ import chainlit as cl
4
  from aimakerspace.vectordatabase import VectorDatabase
5
  from aimakerspace.vectordatabase import asyncio
6
+ from aimakerspace.text_utils import TextFileLoader, CharacterTextSplitter
7
  import os
8
  import openai
9
  from getpass import getpass
 
16
 
17
 
18
  load_dotenv()
19
+ os.environ["OPENAI_API_KEY"] ="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"
20
  openai.api_key = os.environ["OPENAI_API_KEY"]
21
 
 
22
  def load(filename):
23
  text_loader = TextFileLoader(filename)
24
  documents = text_loader.load_documents()
 
26
 
27
  model_name = "gpt-4"
28
 
29
+ filename = "data/KingLear.txt"
30
 
31
  vector_db = VectorDatabase()
32
+ documents = load(filename)
33
+ text_splitter = CharacterTextSplitter()
34
+ split_documents = text_splitter.split_texts(documents)
35
  vector_db = asyncio.run(vector_db.abuild_from_list(split_documents))
36
+
37
+ # prompt templates
38
  user_prompt_template = "{content}"
39
  user_role_prompt = UserRolePrompt(user_prompt_template)
40
  system_prompt_template = (
 
78
 
79
  return self.llm.run([formatted_system_prompt, formatted_user_prompt])
80
 
81
+ async def stream_pipeline(self, user_query: str, message_history: [], msg: cl.Message) -> str:
82
  context_list = self.vector_db_retriever.search_by_text(user_query, k=4)
83
 
84
  context_prompt = ""
 
89
 
90
  formatted_user_prompt = user_prompt.create_message(user_query=user_query)
91
 
92
+ message_history.append(formatted_system_prompt)
93
+ message_history.append(formatted_user_prompt)
94
+
95
+ await self.llm.stream_with_cl_message(message_history=message_history, chainlit_msg=msg)
96
 
97
 
98
  @cl.on_chat_start # marks a function that will be executed at the start of a user session
 
113
 
114
  @cl.on_message # this function will be called every time a user inputs a message in the UI
115
  async def main(message: str):
116
+ message_history = cl.user_session.get("message_history")
117
 
118
+ qaPipeline = RetrievalAugmentedQAPipeline(vector_db_retriever=vector_db, llm=ChatOpenAI(model_name=model_name))
119
  msg = cl.Message(content="")
120
 
121
+ await qaPipeline.stream_pipeline(user_query=message, message_history=message_history, msg=msg)
122
  await msg.send()
123
 
124
 
chainlit.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Welcome to Chainlit! πŸš€πŸ€–
2
+
3
+ Hi there, Developer! πŸ‘‹ We're excited to have you on board. Chainlit is a powerful tool designed to help you prototype, debug and share applications built on top of LLMs.
4
+
5
+ ## Useful Links πŸ”—
6
+
7
+ - **Documentation:** Get started with our comprehensive [Chainlit Documentation](https://docs.chainlit.io) πŸ“š
8
+ - **Discord Community:** Join our friendly [Chainlit Discord](https://discord.gg/k73SQ3FyUh) to ask questions, share your projects, and connect with other developers! πŸ’¬
9
+
10
+ We can't wait to see what you create with Chainlit! Happy coding! πŸ’»πŸ˜Š
11
+
12
+ ## Welcome screen
13
+
14
+ To modify the welcome screen, edit the `chainlit.md` file at the root of your project. If you do not want a welcome screen, just leave this file empty.