Abhishek Ranjan commited on
Commit
bbd5821
Β·
1 Parent(s): 50fb332

add chainlit code

Browse files
Files changed (4) hide show
  1. .gitignore +2 -0
  2. chainlit.md +18 -0
  3. render.yaml +11 -0
  4. src/chainlit_app.py +107 -0
.gitignore CHANGED
@@ -171,3 +171,5 @@ cython_debug/
171
  .pypirc
172
 
173
  .vscode/
 
 
 
171
  .pypirc
172
 
173
  .vscode/
174
+
175
+ .chainlit/
chainlit.md ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Welcome to Artisan Support Chatbot! πŸ‘‹
2
+
3
+ ## Features
4
+
5
+ - Context-aware responses
6
+ - Document-based knowledge
7
+ - Persistent chat history
8
+ - Source references
9
+
10
+ ## Tips
11
+
12
+ 1. Be specific in your questions
13
+ 2. Provide context when needed
14
+ 3. Ask for clarification if needed
15
+
16
+ ## About
17
+
18
+ This chatbot uses RAG (Retrieval Augmented Generation) to provide accurate, context-aware responses.
render.yaml CHANGED
@@ -2,6 +2,7 @@ services:
2
  - type: web
3
  name: artisan-rag-chatbot-api
4
  runtime: python
 
5
  plan: free
6
  autoDeploy: true
7
  envVars:
@@ -15,3 +16,13 @@ services:
15
  sync: false
16
  buildCommand: pip install -r requirements.txt
17
  startCommand: uvicorn src.app:app --host 0.0.0.0 --port $PORT
 
 
 
 
 
 
 
 
 
 
 
2
  - type: web
3
  name: artisan-rag-chatbot-api
4
  runtime: python
5
+ repo: https://github.com/AbhishekRP2002/artisan-chatbot
6
  plan: free
7
  autoDeploy: true
8
  envVars:
 
16
  sync: false
17
  buildCommand: pip install -r requirements.txt
18
  startCommand: uvicorn src.app:app --host 0.0.0.0 --port $PORT
19
+
20
+ - type: web
21
+ name: Chainlit-Artisan-Assistant
22
+ runtime: python
23
+ repo: https://github.com/AbhishekRP2002/artisan-chatbot
24
+ plan: free
25
+ autoDeploy: true
26
+ region: frankfurt
27
+ buildCommand: pip install -r requirements.txt
28
+ startCommand: chainlit run src/app.py -h --port $PORT
src/chainlit_app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chainlit as cl
2
+ import httpx
3
+ import json # noqa
4
+ from typing import Dict
5
+ from dotenv import load_dotenv
6
+ import uuid
7
+ import logging
8
+
9
+ logging.basicConfig(
10
+ level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
11
+ )
12
+ logger = logging.getLogger(__name__)
13
+
14
+ load_dotenv()
15
+
16
+ API_ENDPOINT = "https://artisan-chatbot.onrender.com/chat"
17
+
18
+
19
+ class ChatAPI:
20
+ def __init__(self):
21
+ self.client = httpx.AsyncClient(
22
+ headers={"Content-Type": "application/json"},
23
+ timeout=30.0,
24
+ )
25
+
26
+ async def send_message(self, message: str, session_id: str) -> Dict:
27
+ try:
28
+ response = await self.client.post(
29
+ API_ENDPOINT, json={"message": message, "session_id": session_id}
30
+ )
31
+ response.raise_for_status()
32
+ return response.json()
33
+ except httpx.HTTPError as e:
34
+ raise Exception(f"HTTP error occurred: {str(e)}")
35
+ except Exception as e:
36
+ raise Exception(f"Error sending message: {str(e)}")
37
+
38
+
39
+ chat_api = ChatAPI()
40
+
41
+
42
+ @cl.set_starters
43
+ async def set_starters():
44
+ return [
45
+ cl.Starter(
46
+ label="Can Ava access my CRM?",
47
+ message="Can Ava access my CRM?",
48
+ ),
49
+ cl.Starter(
50
+ label="Ava, the Top-Rated AI SDR on the market",
51
+ message="How can Ava help in automating my SDR workflows in my sales pipelines or my outbound demand generation process?",
52
+ ),
53
+ cl.Starter(
54
+ label="Create a Campaign",
55
+ message="How can Artisan help in creating a campaign to engage potential leads effectively?",
56
+ ),
57
+ cl.Starter(
58
+ label="Generate Sample Email",
59
+ message="Explain the 'Generate Sample Email feature and how to use it via the Artisan Platform.",
60
+ ),
61
+ ]
62
+
63
+
64
+ # @cl.on_chat_start
65
+ # async def start():
66
+ # """
67
+ # Initializes the chat session.
68
+ # """
69
+ # await cl.Message(
70
+ # content="πŸ‘‹ Hello! I'm your Artisan AI Support Assistant. How can I help you today?",
71
+ # ).send()
72
+
73
+
74
+ @cl.on_message
75
+ async def main(message: cl.Message):
76
+ """
77
+ Handles incoming chat messages.
78
+ """
79
+ session_id = cl.user_session.get("id")
80
+ if not session_id:
81
+ session_id = str(uuid.uuid4())
82
+ cl.user_session.set("id", session_id)
83
+
84
+ try:
85
+ response = await chat_api.send_message(
86
+ message=message.content, session_id=session_id
87
+ )
88
+ logger.info(f"API Response: {response}")
89
+ # Update the message with the API response
90
+ llm_response = response.get("response", "No response received.")
91
+ await cl.Message(content=llm_response).send()
92
+
93
+ # If source references are available, append them as additional elements
94
+ # metadata = response.get("metadata", {})
95
+ # if "sources" in metadata:
96
+ # sources = metadata["sources"]
97
+ # elements = [
98
+ # cl.Text(name=f"Source {i+1}", content=source)
99
+ # for i, source in enumerate(sources)
100
+ # ]
101
+ # await msg.update(elements=elements)
102
+
103
+ except Exception as e:
104
+ error_msg = f"Error: {str(e)}"
105
+ msg = cl.Message(content=error_msg)
106
+ await msg.update()
107
+ await cl.ErrorMessage(content=error_msg).send()