AlexO164 commited on
Commit
a7a264b
·
verified ·
1 Parent(s): f1c1d96

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +28 -11
  2. app.py +36 -20
  3. requirements.txt +9 -6
  4. run.py +4 -0
Dockerfile CHANGED
@@ -1,11 +1,28 @@
1
- FROM python:3.11
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"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python image as the base image
2
+ FROM python:3.9
3
+
4
+ # Create a new user with UID 1000
5
+ RUN useradd -m -u 1000 user
6
+
7
+ # Switch to the new user
8
+ USER user
9
+
10
+ # Set environment variables
11
+ ENV HOME=/home/user \
12
+ PATH=/home/user/.local/bin:$PATH
13
+
14
+ # Set the working directory
15
+ WORKDIR $HOME/app
16
+
17
+ # Copy the requirements file and install dependencies
18
+ COPY --chown=user requirements.txt $HOME/app/requirements.txt
19
+ RUN pip install --no-cache-dir -r requirements.txt
20
+
21
+ # Copy the rest of the application code
22
+ COPY --chown=user . $HOME/app
23
+
24
+ # Expose the port Chainlit will run on
25
+ EXPOSE 7860
26
+
27
+ # Run the Chainlit application
28
+ CMD ["python", "run.py"]
app.py CHANGED
@@ -5,6 +5,8 @@ from langchain_pinecone import PineconeVectorStore
5
  import chainlit as cl
6
  from dotenv import load_dotenv
7
  from openai import AsyncOpenAI
 
 
8
 
9
  # Load environment variables
10
  load_dotenv()
@@ -36,6 +38,17 @@ settings = {
36
  "temperature": 0,
37
  }
38
 
 
 
 
 
 
 
 
 
 
 
 
39
  # Welcome message
40
  welcome_message = """
41
  Hello! I am the Teagasc Staff Handbook helper, a large language model trained to assist you with information from the Teagasc Staff Handbook.
@@ -63,7 +76,6 @@ User Query: {query}
63
  Sources: {sources}
64
  """
65
 
66
-
67
  @cl.on_message
68
  async def on_message(message: cl.Message):
69
  # Set up the Pinecone vector store with the existing index and embeddings
@@ -79,6 +91,10 @@ async def on_message(message: cl.Message):
79
  print(f"Found {len(docs)} documents.")
80
  for doc in docs:
81
  print(f"Document metadata: {doc.metadata}")
 
 
 
 
82
  except Exception as e:
83
  await cl.Message(content=f"Error during similarity search: {str(e)}").send()
84
  print(f"Error during similarity search: {str(e)}")
@@ -88,7 +104,7 @@ async def on_message(message: cl.Message):
88
  await cl.Message(content="No relevant documents found.").send()
89
  return
90
 
91
- # Extract the content and source URL from the matching documents
92
  context = "\n\n".join([doc.metadata.get("page_content", "No content") for doc in docs])
93
  sources = "\n".join([doc.metadata.get("source_url", "Unknown source") for doc in docs])
94
 
@@ -98,21 +114,21 @@ async def on_message(message: cl.Message):
98
 
99
  prompt = prompt_template.format(query=message.content, context=context, sources=sources)
100
 
101
- response = await client.chat.completions.create(
102
- messages=[
103
- {
104
- "content": prompt,
105
- "role": "system"
106
- },
107
- {
108
- "content": message.content,
109
- "role": "user"
110
- }
111
- ],
112
- **settings
113
- )
114
-
115
- await cl.Message(content=response.choices[0].message.content).send()
116
-
117
-
118
-
 
5
  import chainlit as cl
6
  from dotenv import load_dotenv
7
  from openai import AsyncOpenAI
8
+ import socketio
9
+ from fastapi import FastAPI
10
 
11
  # Load environment variables
12
  load_dotenv()
 
38
  "temperature": 0,
39
  }
40
 
41
+ # Initialize Socket.IO server with custom ping timeout and interval
42
+ sio = socketio.AsyncServer(
43
+ async_mode='asgi',
44
+ ping_timeout=5, # Time in seconds before a ping is considered timed out
45
+ ping_interval=25 # Time in seconds between pings
46
+ )
47
+
48
+ # Initialize FastAPI app and mount the Socket.IO app
49
+ app = FastAPI()
50
+ app.mount('/', socketio.ASGIApp(sio))
51
+
52
  # Welcome message
53
  welcome_message = """
54
  Hello! I am the Teagasc Staff Handbook helper, a large language model trained to assist you with information from the Teagasc Staff Handbook.
 
76
  Sources: {sources}
77
  """
78
 
 
79
  @cl.on_message
80
  async def on_message(message: cl.Message):
81
  # Set up the Pinecone vector store with the existing index and embeddings
 
91
  print(f"Found {len(docs)} documents.")
92
  for doc in docs:
93
  print(f"Document metadata: {doc.metadata}")
94
+ except KeyError as ke:
95
+ await cl.Message(content=f"Session error: {str(ke)}").send()
96
+ print(f"Session error: {str(ke)}")
97
+ return
98
  except Exception as e:
99
  await cl.Message(content=f"Error during similarity search: {str(e)}").send()
100
  print(f"Error during similarity search: {str(e)}")
 
104
  await cl.Message(content="No relevant documents found.").send()
105
  return
106
 
107
+ # Extract the content and source URL from the matching documents
108
  context = "\n\n".join([doc.metadata.get("page_content", "No content") for doc in docs])
109
  sources = "\n".join([doc.metadata.get("source_url", "Unknown source") for doc in docs])
110
 
 
114
 
115
  prompt = prompt_template.format(query=message.content, context=context, sources=sources)
116
 
117
+ try:
118
+ response = await client.chat.completions.create(
119
+ messages=[
120
+ {
121
+ "content": prompt,
122
+ "role": "system"
123
+ },
124
+ {
125
+ "content": message.content,
126
+ "role": "user"
127
+ }
128
+ ],
129
+ **settings
130
+ )
131
+ await cl.Message(content=response.choices[0].message.content).send()
132
+ except Exception as e:
133
+ await cl.Message(content=f"Error generating response: {str(e)}").send()
134
+ print(f"Error generating response: {str(e)}")
requirements.txt CHANGED
@@ -1,6 +1,9 @@
1
- chainlit
2
- langchain_openai
3
- langchain_pinecone
4
- openai
5
- pinecone
6
- python-dotenv
 
 
 
 
1
+ fastapi==0.95.0
2
+ uvicorn==0.22.0
3
+ python-socketio[asyncio_engineio]==5.8.0
4
+ python-engineio==4.5.1
5
+ chainlit==0.6.1
6
+ openai==0.27.0
7
+ langchain-openai==0.1.6
8
+ pinecone-client==2.2.1
9
+ python-dotenv==1.0.0
run.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ import uvicorn
2
+
3
+ if __name__ == "__main__":
4
+ uvicorn.run("app:app", host="0.0.0.0", port=7860, workers=4)