gizemsarsinlar commited on
Commit
487a8d5
·
verified ·
1 Parent(s): 63dbe21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -65
app.py CHANGED
@@ -1,80 +1,105 @@
1
- # You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)
2
-
3
- # OpenAI Chat completion
4
  import os
5
- from openai import AsyncOpenAI # importing openai for API usage
6
- import chainlit as cl # importing chainlit for our app
7
- from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
8
- from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools
9
- from dotenv import load_dotenv
10
 
11
- api_key = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
 
12
 
13
- # ChatOpenAI Templates
14
- system_template = """You are a helpful assistant who always speaks in a pleasant tone!
15
- """
16
 
17
- user_template = """{input}
18
- Think through your response step by step.
19
- """
20
 
 
21
 
22
- @cl.on_chat_start # marks a function that will be executed at the start of a user session
23
- async def start_chat():
24
- settings = {
25
- "model": "gpt-3.5-turbo",
26
- "temperature": 0,
27
- "max_tokens": 500,
28
- "top_p": 1,
29
- "frequency_penalty": 0,
30
- "presence_penalty": 0,
31
- }
32
 
33
- cl.user_session.set("settings", settings)
34
 
 
 
 
35
 
36
- @cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
37
- async def main(message: cl.Message):
38
- settings = cl.user_session.get("settings")
39
-
40
- client = AsyncOpenAI()
41
-
42
- print(message.content)
43
-
44
- prompt = Prompt(
45
- provider=ChatOpenAI.id,
46
- messages=[
47
- PromptMessage(
48
- role="system",
49
- template=system_template,
50
- formatted=system_template,
51
- ),
52
- PromptMessage(
53
- role="user",
54
- template=user_template,
55
- formatted=user_template.format(input=message.content),
56
- ),
57
- ],
58
- inputs={"input": message.content},
59
- settings=settings,
 
 
 
 
 
 
 
 
 
 
 
 
60
  )
61
 
62
- print([m.to_openai() for m in prompt.messages])
 
 
 
 
 
 
 
63
 
64
- msg = cl.Message(content="")
 
 
65
 
66
- # Call OpenAI
67
- async for stream_resp in await client.chat.completions.create(
68
- messages=[m.to_openai() for m in prompt.messages], stream=True, **settings
69
- ):
70
- token = stream_resp.choices[0].delta.content
71
- if not token:
72
- token = ""
73
- await msg.stream_token(token)
74
 
75
- # Update the prompt object with the completion
76
- prompt.completion = msg.content
77
- msg.prompt = prompt
78
 
79
- # Send and close the message stream
80
- await msg.send()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ from typing import List
 
 
 
 
3
 
4
+ from langchain.embeddings.openai import OpenAIEmbeddings
5
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
6
+ from langchain.vectorstores import Chroma
7
+ from langchain.chains import (
8
+ ConversationalRetrievalChain,
9
+ )
10
+ from langchain.chat_models import ChatOpenAI
11
 
12
+ from langchain.docstore.document import Document
13
+ from langchain.memory import ChatMessageHistory, ConversationBufferMemory
 
14
 
15
+ import chainlit as cl
 
 
16
 
17
+ os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY"
18
 
19
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
 
 
 
 
 
 
 
 
 
20
 
 
21
 
22
+ @cl.on_chat_start
23
+ async def on_chat_start():
24
+ files = None
25
 
26
+ # Wait for the user to upload a file
27
+ while files == None:
28
+ files = await cl.AskFileMessage(
29
+ content="Please upload a text file to begin!",
30
+ accept=["text/plain"],
31
+ max_size_mb=20,
32
+ timeout=180,
33
+ ).send()
34
+
35
+ file = files[0]
36
+
37
+ msg = cl.Message(content=f"Processing `{file.name}`...")
38
+ await msg.send()
39
+
40
+ with open(file.path, "r", encoding="utf-8") as f:
41
+ text = f.read()
42
+
43
+ # Split the text into chunks
44
+ texts = text_splitter.split_text(text)
45
+
46
+ # Create a metadata for each chunk
47
+ metadatas = [{"source": f"{i}-pl"} for i in range(len(texts))]
48
+
49
+ # Create a Chroma vector store
50
+ embeddings = OpenAIEmbeddings()
51
+ docsearch = await cl.make_async(Chroma.from_texts)(
52
+ texts, embeddings, metadatas=metadatas
53
+ )
54
+
55
+ message_history = ChatMessageHistory()
56
+
57
+ memory = ConversationBufferMemory(
58
+ memory_key="chat_history",
59
+ output_key="answer",
60
+ chat_memory=message_history,
61
+ return_messages=True,
62
  )
63
 
64
+ # Create a chain that uses the Chroma vector store
65
+ chain = ConversationalRetrievalChain.from_llm(
66
+ ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, streaming=True),
67
+ chain_type="stuff",
68
+ retriever=docsearch.as_retriever(),
69
+ memory=memory,
70
+ return_source_documents=True,
71
+ )
72
 
73
+ # Let the user know that the system is ready
74
+ msg.content = f"Processing `{file.name}` done. You can now ask questions!"
75
+ await msg.update()
76
 
77
+ cl.user_session.set("chain", chain)
 
 
 
 
 
 
 
78
 
 
 
 
79
 
80
+ @cl.on_message
81
+ async def main(message: cl.Message):
82
+ chain = cl.user_session.get("chain") # type: ConversationalRetrievalChain
83
+ cb = cl.AsyncLangchainCallbackHandler()
84
+
85
+ res = await chain.acall(message.content, callbacks=[cb])
86
+ answer = res["answer"]
87
+ source_documents = res["source_documents"] # type: List[Document]
88
+
89
+ text_elements = [] # type: List[cl.Text]
90
+
91
+ if source_documents:
92
+ for source_idx, source_doc in enumerate(source_documents):
93
+ source_name = f"source_{source_idx}"
94
+ # Create the text element referenced in the message
95
+ text_elements.append(
96
+ cl.Text(content=source_doc.page_content, name=source_name, display="side")
97
+ )
98
+ source_names = [text_el.name for text_el in text_elements]
99
+
100
+ if source_names:
101
+ answer += f"\nSources: {', '.join(source_names)}"
102
+ else:
103
+ answer += "\nNo sources found"
104
+
105
+ await cl.Message(content=answer, elements=text_elements).send()