philmui commited on
Commit
3d83ae3
β€’
1 Parent(s): 608d4bc

add app files

Browse files
Files changed (3) hide show
  1. Dockerfile +11 -0
  2. chainlit.md +14 -0
  3. chat.py +67 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["chainlit", "run", "chat.py", "-w", "--port", "7860"]
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.
chat.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import chainlit as cl
3
+ import asyncio
4
+
5
+ from langchain_openai.chat_models import ChatOpenAI
6
+ from langchain_core.output_parsers import StrOutputParser
7
+ from langchain_core.prompts import ChatPromptTemplate
8
+ from langchain_core.messages import (
9
+ HumanMessage,
10
+ SystemMessage
11
+ )
12
+
13
+ MESSAGE_HISTORY = "message_history"
14
+ GPT3_MODEL_NAME = "gpt-3.5-turbo-0125"
15
+ GPT4_MODEL_NAME = "gpt-4-turbo-preview"
16
+ OPENAI_API_KEY=os.environ.get("OPENAI_API_KEY")
17
+
18
+ print(f"==> OPENAI_API_KEY: {OPENAI_API_KEY}")
19
+ _system_message = SystemMessage(content=(
20
+ "You are a helpful and creative assistant who tries your best to answer questions "
21
+ " in the most witty and funny way. If you feel that a poetic touch is neede to "
22
+ " uplift the mood for the user, go ahead to write a sonnet. Always be positive, "
23
+ " encouraging, and inspirational if possible."
24
+ ))
25
+ _chat_model: ChatOpenAI
26
+
27
+ @cl.on_chat_start
28
+ async def start():
29
+ global _chat_model
30
+ _chat_model = ChatOpenAI(
31
+ model=GPT4_MODEL_NAME,
32
+ temperature=0.5
33
+ )
34
+ cl.user_session.set(MESSAGE_HISTORY, [_system_message])
35
+ await cl.Message(
36
+ content="Hello there! How are you?"
37
+ ).send()
38
+
39
+ @cl.on_message
40
+ async def main(message: cl.Message):
41
+
42
+ messages = cl.user_session.get(MESSAGE_HISTORY)
43
+ if not messages:
44
+ messages = [_system_message]
45
+
46
+ if len(message.elements) > 0:
47
+ for element in message.elements:
48
+ with open(element.path, "r") as uploaded_file:
49
+ content = uploaded_file.read()
50
+ messages.append(HumanMessage(content=content))
51
+ confirm_message = cl.Message(content=f"Uploaded file: {element.name}")
52
+ await confirm_message.send()
53
+
54
+ messages.append(HumanMessage(content=message.content))
55
+ prompt = ChatPromptTemplate.from_messages(messages=messages)
56
+ print(vars(prompt))
57
+ output_parser = StrOutputParser()
58
+
59
+ try:
60
+ chain = prompt | _chat_model | output_parser
61
+ response = chain.invoke({"input": message.content})
62
+ except Exception as e:
63
+ response = f"no response: {e}"
64
+
65
+ await cl.Message(
66
+ content=response
67
+ ).send()