gperdrizet commited on
Commit
68aa226
·
verified ·
1 Parent(s): 4facc97

Basic tool calling works.

Browse files
Files changed (2) hide show
  1. client/anthropic_bridge.py +4 -3
  2. rss_client.py +54 -3
client/anthropic_bridge.py CHANGED
@@ -192,9 +192,10 @@ class AnthropicBridge(LLMBridge):
192
  model=self.model,
193
  max_tokens=4096,
194
  system='You are a helpful tool-using assistant.',
195
- messages=[
196
- {'role': 'user', 'content': query}
197
- ],
 
198
  tools=formatted_tools
199
  )
200
 
 
192
  model=self.model,
193
  max_tokens=4096,
194
  system='You are a helpful tool-using assistant.',
195
+ # messages=[
196
+ # {'role': 'user', 'content': query}
197
+ # ],
198
+ messages=query,
199
  tools=formatted_tools
200
  )
201
 
rss_client.py CHANGED
@@ -1,11 +1,13 @@
1
  '''RSS MCP server demonstration client app.'''
2
 
3
  import os
 
4
  import logging
5
  from pathlib import Path
6
  from logging.handlers import RotatingFileHandler
7
 
8
  import gradio as gr
 
9
  from client.mcp_client import MCPClientWrapper
10
  from client.anthropic_bridge import AnthropicBridge
11
 
@@ -39,14 +41,63 @@ async def submit_input(message: str, chat_history: list) -> str:
39
 
40
  function_logger = logging.getLogger(__name__ + '.submit_input')
41
 
42
- result = await bridge.process_query(message)
43
- function_logger.info(result)
44
  chat_history.append({"role": "user", "content": message})
45
- chat_history.append({"role": "assistant", "content": result['llm_response'].content[0].text})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  return '', chat_history
48
 
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  with gr.Blocks(title='MCP RSS client') as demo:
51
  gr.Markdown('# MCP RSS reader')
52
  gr.Markdown(
 
1
  '''RSS MCP server demonstration client app.'''
2
 
3
  import os
4
+ import json
5
  import logging
6
  from pathlib import Path
7
  from logging.handlers import RotatingFileHandler
8
 
9
  import gradio as gr
10
+ from gradio.components.chatbot import ChatMessage
11
  from client.mcp_client import MCPClientWrapper
12
  from client.anthropic_bridge import AnthropicBridge
13
 
 
41
 
42
  function_logger = logging.getLogger(__name__ + '.submit_input')
43
 
 
 
44
  chat_history.append({"role": "user", "content": message})
45
+ input_messages = format_chat_history(chat_history)
46
+ function_logger.info(input_messages)
47
+
48
+ result = await bridge.process_query(input_messages)
49
+ function_logger.info(result)
50
+ function_logger.info(result.keys())
51
+
52
+ try:
53
+ chat_history.append({
54
+ "role": "assistant",
55
+ "content": result['llm_response'].content[0].text
56
+ })
57
+
58
+ except AttributeError:
59
+ function_logger.info('Model called the tool, but did not talk about it')
60
+
61
+ if result['tool_result']:
62
+ articles = json.loads(result['tool_result'].content)['text']
63
+ function_logger.info(articles)
64
+ tmp_chat_history = chat_history.copy()
65
+ tmp_chat_history.append({
66
+ "role": "assistant",
67
+ "content": ('Here are the three most recent entries from the RSS ' +
68
+ f'feed in JSON format. Tell the user what you have found: {json.dumps(articles)}')
69
+ })
70
+
71
+ tmp_input_messages = format_chat_history(tmp_chat_history)
72
+ function_logger.info(tmp_input_messages)
73
+ result = await bridge.process_query(tmp_input_messages)
74
+
75
+ chat_history.append({
76
+ "role": "assistant",
77
+ "content": result['llm_response'].content[0].text
78
+ })
79
+
80
 
81
  return '', chat_history
82
 
83
 
84
+ def format_chat_history(history) -> list[dict]:
85
+ '''Formats gradio chat history for submission to anthropic.'''
86
+
87
+ messages = []
88
+
89
+ for chat_message in history:
90
+ if isinstance(msg, ChatMessage):
91
+ role, content = chat_message.role, chat_message.content
92
+ else:
93
+ role, content = chat_message.get("role"), chat_message.get("content")
94
+
95
+ if role in ["user", "assistant", "system"]:
96
+ messages.append({"role": role, "content": content})
97
+
98
+ return messages
99
+
100
+
101
  with gr.Blocks(title='MCP RSS client') as demo:
102
  gr.Markdown('# MCP RSS reader')
103
  gr.Markdown(