gizemsarsinlar commited on
Commit
e394ab6
·
verified ·
1 Parent(s): 1b84a91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -14
app.py CHANGED
@@ -1,25 +1,30 @@
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",
@@ -32,14 +37,35 @@ async def start_chat():
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,
@@ -55,12 +81,10 @@ async def main(message: cl.Message):
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
@@ -78,3 +102,4 @@ async def main(message: cl.Message):
78
 
79
  # Send and close the message stream
80
  await msg.send()
 
 
 
 
 
1
  import os
2
  from openai import AsyncOpenAI # importing openai for API usage
3
  import chainlit as cl # importing chainlit for our app
4
  from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
5
  from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools
6
  from dotenv import load_dotenv
7
+ import asyncio
8
+ import textract # importing textract for document processing
9
 
10
+ # Load OpenAI API key from environment variables
11
  api_key = os.getenv("OPENAI_API_KEY")
12
 
13
+ # Templates for ChatOpenAI interaction
14
+ system_template = """You are a helpful assistant who always speaks in a pleasant tone and answers based on the provided document!
15
  """
16
 
17
  user_template = """{input}
18
+ Use the document content to respond to the user query step by step.
19
  """
20
 
21
+ # Function to extract text from uploaded documents
22
+ async def extract_text_from_file(file):
23
+ # Extract text from the uploaded file using textract
24
+ return textract.process(file).decode('utf-8')
25
 
26
+ # Function to start the chat session
27
+ @cl.on_chat_start
28
  async def start_chat():
29
  settings = {
30
  "model": "gpt-3.5-turbo",
 
37
 
38
  cl.user_session.set("settings", settings)
39
 
40
+ # Welcome message with file upload option
41
+ await cl.Message(content="Welcome! Please upload a document to begin.").send()
42
+
43
+ # Function to handle uploaded files
44
+ @cl.on_file_upload
45
+ async def on_file_upload(files: list):
46
+ # Handle the uploaded files, assuming it's the first file
47
+ if files:
48
+ file = files[0]
49
+ file_content = await extract_text_from_file(file.path)
50
+
51
+ # Save document content in session for later use
52
+ cl.user_session.set("document_content", file_content)
53
+
54
+ # Inform the user that the document was successfully uploaded
55
+ await cl.Message(content=f"Document '{file.name}' uploaded successfully! You can now ask questions based on the document content.").send()
56
+
57
+ # Function to handle user messages
58
+ @cl.on_message
59
  async def main(message: cl.Message):
60
  settings = cl.user_session.get("settings")
61
+ document_content = cl.user_session.get("document_content", "")
62
 
63
+ if not document_content:
64
+ # If no document is uploaded, prompt the user to upload one
65
+ await cl.Message(content="Please upload a document first.").send()
66
+ return
67
 
68
+ client = AsyncOpenAI()
69
 
70
  prompt = Prompt(
71
  provider=ChatOpenAI.id,
 
81
  formatted=user_template.format(input=message.content),
82
  ),
83
  ],
84
+ inputs={"input": message.content, "document": document_content},
85
  settings=settings,
86
  )
87
 
 
 
88
  msg = cl.Message(content="")
89
 
90
  # Call OpenAI
 
102
 
103
  # Send and close the message stream
104
  await msg.send()
105
+