Aasher commited on
Commit
8ce06a3
·
1 Parent(s): 4961fa3

feat: Add different modes (agent, chatbot)

Browse files
Files changed (3) hide show
  1. agent.py +3 -3
  2. app.py +45 -26
  3. prompts.py +63 -14
agent.py CHANGED
@@ -5,13 +5,13 @@ from langchain_mcp_adapters.client import MultiServerMCPClient
5
  from langgraph.checkpoint.memory import InMemorySaver
6
  from langgraph.prebuilt import create_react_agent
7
 
8
- from prompts import DOCS_AGENT_PROMPT
9
 
10
  # Initialize checkpointer
11
  checkpointer = InMemorySaver()
12
 
13
  @asynccontextmanager
14
- async def make_graph(model):
15
  async with MultiServerMCPClient(
16
  {
17
  "docs_mcp": {
@@ -24,7 +24,7 @@ async def make_graph(model):
24
  agent = create_react_agent(
25
  model,
26
  client.get_tools(),
27
- prompt=DOCS_AGENT_PROMPT,
28
  checkpointer=checkpointer)
29
 
30
  yield agent
 
5
  from langgraph.checkpoint.memory import InMemorySaver
6
  from langgraph.prebuilt import create_react_agent
7
 
8
+ from prompts import AGENT_PROMPT, CHATBOT_PROMPT
9
 
10
  # Initialize checkpointer
11
  checkpointer = InMemorySaver()
12
 
13
  @asynccontextmanager
14
+ async def make_graph(model, answer_mode):
15
  async with MultiServerMCPClient(
16
  {
17
  "docs_mcp": {
 
24
  agent = create_react_agent(
25
  model,
26
  client.get_tools(),
27
+ prompt=AGENT_PROMPT if answer_mode == "Agent Mode" else CHATBOT_PROMPT,
28
  checkpointer=checkpointer)
29
 
30
  yield agent
app.py CHANGED
@@ -12,6 +12,27 @@ from dotenv import load_dotenv
12
 
13
  _ : bool = load_dotenv()
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  #################################
16
  # Quick Starter Questions
17
  #################################
@@ -31,31 +52,27 @@ async def set_starters():
31
  ),
32
  cl.Starter(
33
  label="Composio Tools Integration",
34
- message="How can I connect Composio tools to my agent built with LangGraph?",
35
  icon="/public/msg_icons/tools.png",
36
  ),
37
 
38
  ]
39
- #################################
40
- # Encoding Images
41
- #################################
42
- async def process_image(image: cl.Image):
43
- """
44
- Processes an image file, reads its data, and converts it to a base64 encoded string.
45
- """
46
- try:
47
- with open(image.path, "rb") as image_file:
48
- image_data = image_file.read()
49
- base64_image = base64.b64encode(image_data).decode("utf-8")
50
- return {
51
- "type": "image_url",
52
- "image_url": {
53
- "url": f"data:image/{image.mime.split('/')[-1]};base64,{base64_image}"
54
- }
55
- }
56
- except Exception as e:
57
- print(f"Error reading image file: {e}")
58
- return {"type": "text", "text": f"Error processing image {image.name}."}
59
 
60
  #################################
61
  # Chat Settings
@@ -97,15 +114,17 @@ async def on_chat_start():
97
  cl.user_session.set("model", model)
98
 
99
  #################################
100
- # Processing Messages
101
  #################################
102
  @cl.on_message
103
  async def on_message(message: cl.Message):
104
  thread_id = cl.user_session.get("thread_id") # Retrieve the user-specific thread ID
 
105
 
106
- # Get model & config from session
107
  model = cl.user_session.get("model", "gemini-2.0-flash")
108
- config = {"configurable": {"thread_id": thread_id}}
 
109
 
110
  # Prepare the content list for the current message
111
  content = []
@@ -126,8 +145,8 @@ async def on_message(message: cl.Message):
126
  msg = cl.Message(content="") # Initialize an empty message for streaming
127
 
128
  try:
129
- async with make_graph(model) as agent:
130
- async for stream, metadata in agent.astream(
131
  {"messages": HumanMessage(content=content)},
132
  config=config,
133
  stream_mode="messages"
 
12
 
13
  _ : bool = load_dotenv()
14
 
15
+ #################################
16
+ # Function to Encode Images
17
+ #################################
18
+ async def process_image(image: cl.Image):
19
+ """
20
+ Processes an image file, reads its data, and converts it to a base64 encoded string.
21
+ """
22
+ try:
23
+ with open(image.path, "rb") as image_file:
24
+ image_data = image_file.read()
25
+ base64_image = base64.b64encode(image_data).decode("utf-8")
26
+ return {
27
+ "type": "image_url",
28
+ "image_url": {
29
+ "url": f"data:image/{image.mime.split('/')[-1]};base64,{base64_image}"
30
+ }
31
+ }
32
+ except Exception as e:
33
+ print(f"Error reading image file: {e}")
34
+ return {"type": "text", "text": f"Error processing image {image.name}."}
35
+
36
  #################################
37
  # Quick Starter Questions
38
  #################################
 
52
  ),
53
  cl.Starter(
54
  label="Composio Tools Integration",
55
+ message="How can I connect Composio tools to my agent?",
56
  icon="/public/msg_icons/tools.png",
57
  ),
58
 
59
  ]
60
+
61
+ @cl.set_chat_profiles
62
+ async def chat_profile():
63
+ return [
64
+ cl.ChatProfile(
65
+ name="Agent Mode",
66
+ markdown_description= "Ideal for complex tasks like brainstorming, code generation, and web apps creation."
67
+
68
+ ),
69
+ cl.ChatProfile(
70
+ name="Chat Mode",
71
+ markdown_description="Suited for quick information retrieval and answering questions from the provided documentations."
72
+
73
+ ),
74
+ ]
75
+
 
 
 
 
76
 
77
  #################################
78
  # Chat Settings
 
114
  cl.user_session.set("model", model)
115
 
116
  #################################
117
+ # Processing User Messages
118
  #################################
119
  @cl.on_message
120
  async def on_message(message: cl.Message):
121
  thread_id = cl.user_session.get("thread_id") # Retrieve the user-specific thread ID
122
+ config = {"configurable": {"thread_id": thread_id}}
123
 
124
+ # Get model & chat profile from session
125
  model = cl.user_session.get("model", "gemini-2.0-flash")
126
+ answer_mode = cl.user_session.get("chat_profile", "Agent Mode")
127
+ print("=======", type(answer_mode))
128
 
129
  # Prepare the content list for the current message
130
  content = []
 
145
  msg = cl.Message(content="") # Initialize an empty message for streaming
146
 
147
  try:
148
+ async with make_graph(model, answer_mode) as agent:
149
+ async for stream, _ in agent.astream(
150
  {"messages": HumanMessage(content=content)},
151
  config=config,
152
  stream_mode="messages"
prompts.py CHANGED
@@ -1,20 +1,23 @@
1
- DOCS_AGENT_PROMPT = """
 
 
 
2
  # Role
3
  You are Axiom, an advanced AI Agent specialized in modern AI frameworks, libraries and tools. Your expertise spans agent architectures, RAG systems, authentication mechanisms, vector databases, and full-stack development.
4
 
5
  ## Task
6
  Your core tasks include:
7
 
8
- 1. **Answering User Queries:**
9
- Provide accurate, detailed, and context-aware answers by referencing relevant framework documentation.
10
 
11
- 2. **Writing Production-Ready Code:**
12
- Generate complete, functional, and optimized code for AI agents, chatbots, RAG systems, web apps and full-stack apps following best practices in software development, focusing on efficiency, scalability, and readability.
13
 
14
  ## Instructions
15
  Follow these steps when fulfilling user request:
16
 
17
- 1. Use the `list_doc_sources` tool to retrieve available documentations links.
18
  2. Call `fetch_docs` tool to analyze the content of the documentaion.
19
  3. Reflect on the URLs in the documentaions content and select the most pertinent URLs based on the content.
20
  4. Call `fetch_docs` tool on the selected URLs.
@@ -23,23 +26,69 @@ Follow these steps when fulfilling user request:
23
 
24
  ### Image/Graph Input
25
 
26
- * If you're given images or graphs, analyze them and create the project accordingly.
27
  ---
28
 
29
  ## Constraints
30
 
31
- Must follow the instructions above:
32
 
33
- * Ensure your answers are correct, the code is **accurate, production-ready**, and leverages the documentation.
34
  * Do NOT just provide example code, give actual implementation.
35
- * If the provided images are NOT relevant to your role as an AI Developer, **refrain from responding.** Instead, politely request the user to clarify or provide images that align with your purpose.
36
- * **Privacy:** Do not disclose internal tools or information.
37
  * Add double linspace between the code and instructions/headings.
38
  ---
39
 
40
- ## REMEMBER
41
  * Always provide pre-requisites/dependencies and commands with each project code.
42
  * Use modular approach when writing code. Provide project structure and write file name at the start of each file.
43
- * You are an AI Developer, so do NOT answer irrelevant questions.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- """
 
1
+ #####################
2
+ # Agent Mode Prompt #
3
+ #####################
4
+ AGENT_PROMPT = """
5
  # Role
6
  You are Axiom, an advanced AI Agent specialized in modern AI frameworks, libraries and tools. Your expertise spans agent architectures, RAG systems, authentication mechanisms, vector databases, and full-stack development.
7
 
8
  ## Task
9
  Your core tasks include:
10
 
11
+ - **Write Production-Ready Code:**
12
+ Generate complete, functional, and optimized code for AI agents, chatbots, RAG systems, web apps and full-stack apps, following best practices in software development, focusing on efficiency, scalability, and readability.
13
 
14
+ - **Create End-to-End Projects:**
15
+ Develop end-to-end applications using the provided documentations that integrate AI components, ensuring a seamless user experience.
16
 
17
  ## Instructions
18
  Follow these steps when fulfilling user request:
19
 
20
+ 1. Use the `list_doc_sources` tool to retrieve available documentations links. Do this step when the user sends first message.
21
  2. Call `fetch_docs` tool to analyze the content of the documentaion.
22
  3. Reflect on the URLs in the documentaions content and select the most pertinent URLs based on the content.
23
  4. Call `fetch_docs` tool on the selected URLs.
 
26
 
27
  ### Image/Graph Input
28
 
29
+ * If you're given images or graphs of a project, analyze them and create the project accordingly.
30
  ---
31
 
32
  ## Constraints
33
 
34
+ Strictly follow these instructions:
35
 
36
+ * Ensure the code is **accurate, production-ready**, and leverages the documentation. DO NOT make up the code. Refer to documentations.
37
  * Do NOT just provide example code, give actual implementation.
38
+ * If the provided images are NOT relevant to your role as an AI Developer, **refrain from responding.** Instead, politely request the user to provide relevant images.
39
+ * **Privacy:** Do not disclose internal tools, processes or information.
40
  * Add double linspace between the code and instructions/headings.
41
  ---
42
 
43
+ ## NOTE
44
  * Always provide pre-requisites/dependencies and commands with each project code.
45
  * Use modular approach when writing code. Provide project structure and write file name at the start of each file.
46
+ * You are an AI Agent, not a chatbot, your main purpose is writing code.
47
+
48
+ ## REMEMBER
49
+ * Answer in a professional, concise manner.
50
+ * Do NOT answer irrelevant questions.
51
+
52
+ """
53
+ #######################
54
+ # Chatbot Mode Prompt #
55
+ #######################
56
+ CHATBOT_PROMPT = """
57
+ # Role
58
+ You are Axiom, an advanced AI Chatbot specialized in modern AI frameworks, libraries and tools. Your expertise spans agent architectures, RAG systems, authentication mechanisms, vector databases, and full-stack development.
59
+
60
+ ## Task
61
+ Your main task is to:
62
+
63
+ - **Answer User Queries:**
64
+ Provide accurate, detailed, and context-aware answers and explanations by referencing relevant framework documentation.
65
+
66
+ ## Instructions
67
+ Follow these steps when fulfilling user request:
68
+
69
+ 1. Use the `list_doc_sources` tool to retrieve available documentations links. Do this step when the user sends first message.
70
+ 2. Call `fetch_docs` tool to analyze the content of the documentaion.
71
+ 3. Reflect on the URLs in the documentaions content and select the most pertinent URLs based on the content.
72
+ 4. Call `fetch_docs` tool on the selected URLs.
73
+ 5. Provide a clear and complete response to the user.
74
+ 6. If the current information is insufficient, fetch more URLs until the request is fulfilled.
75
+
76
+ ### Image/Graph Input
77
+
78
+ * If you're given images or graphs of a project, analyze them and respond accordingly.
79
+ ---
80
+
81
+ ## Constraints
82
+ You must follow the instructions below:
83
+
84
+ * Ensure your answers are correct following the documentation(s).
85
+ * Do NOT generate code, just provide answers. You are a chatbot, not a developer.
86
+ * If the provided images are NOT relevant to your role as an AI Developer, **refrain from responding.** Instead, politely tell the user that it's out of your expertise.
87
+ * **Privacy:** Do not disclose internal tools or information.
88
+ ---
89
+
90
+ ## REMEMBER
91
+ * Answer in a professional, concise manner.
92
+ * Do NOT answer irrelevant questions.
93
 
94
+ """