lsy9874205 commited on
Commit
bfbb96e
Β·
1 Parent(s): b633713

Fix and update Chainlit application

Browse files
.chainlit/config.toml ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Process and display HTML in messages. This can be a security risk (see https://stackoverflow.com/questions/19603097/why-is-it-dangerous-to-render-user-generated-html-or-javascript)
22
+ unsafe_allow_html = false
23
+
24
+ # Process and display mathematical expressions. This can clash with "$" characters in messages.
25
+ latex = false
26
+
27
+ # Authorize users to upload files with messages
28
+ multi_modal = true
29
+
30
+ # Allows user to use speech to text
31
+ [features.speech_to_text]
32
+ enabled = false
33
+ # See all languages here https://github.com/JamesBrill/react-speech-recognition/blob/HEAD/docs/API.md#language-string
34
+ # language = "en-US"
35
+
36
+ [UI]
37
+ # Name of the app and chatbot.
38
+ name = "Chatbot"
39
+
40
+ # Show the readme while the conversation is empty.
41
+ show_readme_as_default = true
42
+
43
+ # Description of the app and chatbot. This is used for HTML tags.
44
+ # description = ""
45
+
46
+ # Large size content are by default collapsed for a cleaner ui
47
+ default_collapse_content = true
48
+
49
+ # The default value for the expand messages settings.
50
+ default_expand_messages = false
51
+
52
+ # Hide the chain of thought details from the user in the UI.
53
+ hide_cot = false
54
+
55
+ # Link to your github repo. This will add a github button in the UI's header.
56
+ # github = ""
57
+
58
+ # Specify a CSS file that can be used to customize the user interface.
59
+ # The CSS file can be served from the public directory or via an external link.
60
+ # custom_css = "/public/test.css"
61
+
62
+ # Override default MUI light theme. (Check theme.ts)
63
+ [UI.theme.light]
64
+ #background = "#FAFAFA"
65
+ #paper = "#FFFFFF"
66
+
67
+ [UI.theme.light.primary]
68
+ #main = "#F80061"
69
+ #dark = "#980039"
70
+ #light = "#FFE7EB"
71
+
72
+ # Override default MUI dark theme. (Check theme.ts)
73
+ [UI.theme.dark]
74
+ #background = "#FAFAFA"
75
+ #paper = "#FFFFFF"
76
+
77
+ [UI.theme.dark.primary]
78
+ #main = "#F80061"
79
+ #dark = "#980039"
80
+ #light = "#FFE7EB"
81
+
82
+
83
+ [meta]
84
+ generated_by = "0.7.700"
__pycache__/app.cpython-39.pyc ADDED
Binary file (2.13 kB). View file
 
app.py CHANGED
@@ -5,3 +5,85 @@ app = FastAPI()
5
  @app.get("/")
6
  def greet_json():
7
  return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  @app.get("/")
6
  def greet_json():
7
  return {"Hello": "World!"}
8
+ # You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)
9
+
10
+ # OpenAI Chat completion
11
+ import os
12
+ from openai import AsyncOpenAI # importing openai for API usage
13
+ import os
14
+ print("OPENAI_API_KEY:", os.getenv("OPENAI_API_KEY"))
15
+ import chainlit as cl # importing chainlit for our app
16
+ from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
17
+ from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools
18
+ from dotenv import load_dotenv
19
+
20
+ load_dotenv()
21
+
22
+ # ChatOpenAI Templates
23
+ system_template = """You are a helpful assistant who always speaks in a pleasant tone!
24
+ """
25
+
26
+ user_template = """{input}
27
+ Think through your response step by step.
28
+ """
29
+
30
+
31
+ @cl.on_chat_start # marks a function that will be executed at the start of a user session
32
+ async def start_chat():
33
+ settings = {
34
+ "model": "gpt-3.5-turbo",
35
+ "temperature": 0,
36
+ "max_tokens": 500,
37
+ "top_p": 1,
38
+ "frequency_penalty": 0,
39
+ "presence_penalty": 0,
40
+ }
41
+
42
+ cl.user_session.set("settings", settings)
43
+
44
+
45
+ @cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
46
+ async def main(message: cl.Message):
47
+ settings = cl.user_session.get("settings")
48
+
49
+ client = AsyncOpenAI()
50
+
51
+ print(message.content)
52
+
53
+ prompt = Prompt(
54
+ provider=ChatOpenAI.id,
55
+ messages=[
56
+ PromptMessage(
57
+ role="system",
58
+ template=system_template,
59
+ formatted=system_template,
60
+ ),
61
+ PromptMessage(
62
+ role="user",
63
+ template=user_template,
64
+ formatted=user_template.format(input=message.content),
65
+ ),
66
+ ],
67
+ inputs={"input": message.content},
68
+ settings=settings,
69
+ )
70
+
71
+ print([m.to_openai() for m in prompt.messages])
72
+
73
+ msg = cl.Message(content="")
74
+
75
+ # Call OpenAI
76
+ async for stream_resp in await client.chat.completions.create(
77
+ messages=[m.to_openai() for m in prompt.messages], stream=True, **settings
78
+ ):
79
+ token = stream_resp.choices[0].delta.content
80
+ if not token:
81
+ token = ""
82
+ await msg.stream_token(token)
83
+
84
+ # Update the prompt object with the completion
85
+ prompt.completion = msg.content
86
+ msg.prompt = prompt
87
+
88
+ # Send and close the message stream
89
+ await msg.send()
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.
requirements.txt CHANGED
@@ -1,2 +1,9 @@
1
  fastapi
2
  uvicorn[standard]
 
 
 
 
 
 
 
 
1
  fastapi
2
  uvicorn[standard]
3
+ chainlit==0.7.700
4
+ pydantic<2.0.0
5
+ cohere==4.37
6
+ openai==1.3.5
7
+ tiktoken==0.5.1
8
+ python-dotenv==1.0.0
9
+ pydantic-core<2.00