schrilax commited on
Commit
b83b72a
1 Parent(s): 4a21809

initial commit

Browse files
Files changed (4) hide show
  1. Dockerfile +11 -0
  2. app.py +74 -0
  3. chainlit.md +1 -0
  4. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+ RUN useradd -m -u 1000 user
3
+ USER user
4
+ ENV HOME=/home/user \
5
+ PATH=/home/user/.local/bin:$PATH
6
+ WORKDIR $HOME/app
7
+ COPY --chown=user . $HOME/app
8
+ COPY ./requirements.txt ~/app/requirements.txt
9
+ RUN pip install -r requirements.txt
10
+ COPY . .
11
+ CMD ["chainlit", "run", "app.py", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)
2
+
3
+ # OpenAI Chat completion
4
+
5
+ import openai #importing openai for API usage
6
+ import chainlit as cl #importing chainlit for our app
7
+ from chainlit.input_widget import Select, Switch, Slider #importing chainlit settings selection tools
8
+ from chainlit.prompt import Prompt, PromptMessage #importing prompt tools
9
+ from chainlit.playground.providers import ChatOpenAI #importing ChatOpenAI tools
10
+
11
+ # You only need the api key inserted here if it's not in your .env file
12
+ # openai.api_key = ""
13
+
14
+ # ChatOpenAI Templates
15
+ system_template = """You are a helpful assistant who always speaks in a pleasant tone!
16
+ """
17
+
18
+ user_template = """{input}
19
+ Think through your response step by step.
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
+ @cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
36
+ async def main(message: str):
37
+
38
+ settings = cl.user_session.get("settings")
39
+
40
+ prompt = Prompt(
41
+ provider=ChatOpenAI.id,
42
+ messages=[
43
+ PromptMessage(
44
+ role="system",
45
+ template=system_template,
46
+ formatted=system_template,
47
+ ),
48
+ PromptMessage(
49
+ role="user",
50
+ template=user_template,
51
+ formatted=user_template.format(input=message),
52
+ )
53
+ ],
54
+ inputs = {"input" : message},
55
+ settings=settings
56
+ )
57
+
58
+ print([m.to_openai() for m in prompt.messages])
59
+
60
+ msg = cl.Message(content="")
61
+
62
+ # Call OpenAI
63
+ async for stream_resp in await openai.ChatCompletion.acreate(
64
+ messages=[m.to_openai() for m in prompt.messages], stream=True, **settings
65
+ ):
66
+ token = stream_resp.choices[0]["delta"].get("content", "")
67
+ await msg.stream_token(token)
68
+
69
+ # Update the prompt object with the completion
70
+ prompt.completion = msg.content
71
+ msg.prompt = prompt
72
+
73
+ # Send and close the message stream
74
+ await msg.send()
chainlit.md ADDED
@@ -0,0 +1 @@
 
 
1
+ # Simple demo ! 🚀🤖
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ chainlit
2
+ openai