arre99 commited on
Commit
428a745
·
1 Parent(s): 3beee66

Using nebius provider to call Mistral model

Browse files
Files changed (2) hide show
  1. mcp_client.py +14 -12
  2. utils/scripts/inference_test.py +35 -0
mcp_client.py CHANGED
@@ -7,32 +7,35 @@ from smolagents import InferenceClientModel, LiteLLMModel, ToolCallingAgent, MCP
7
  # Can manully set this to a specific time to make the agent think it is in the past
8
  time = datetime.datetime.now().astimezone().isoformat()
9
 
10
- SYSTEM_PROMPT = """You are a helpful Formula 1 assistant and strategist. You have access to various F1 data and tools to help answer questions about races, drivers, teams, and more.
11
  Be concise and accurate in your responses. If you don't know something, use the available tools to find the information.
12
  In addition, you will be asked to act as a live race engineer strategist during a Formula 1 race, making crucial calls during the event.
 
 
 
13
 
14
  Current time (ISO 8601): {time}"""
15
 
16
 
17
  def agent_chat(message: str, history: list):
18
-
19
- # Manually compose messages: system prompt, then history, then current user message
20
- message = f"{SYSTEM_PROMPT}\n{"\n".join([f"{x['role']}: {x['content']}" for x in history])}\nTask: {message}"
21
- return agent.run(message, max_steps=5)
22
 
23
 
24
  if __name__ == "__main__":
25
 
26
- list_tools = True # Set to True to only list tools (used for debugging)
27
- local_model = True # If you have Ollama installed, set this to True
28
  openf1_tool_only = True
29
 
30
  try:
31
 
 
32
  mcp_client = MCPClient(
33
  {"url": "https://agents-mcp-hackathon-f1-mcp-server.hf.space/gradio_api/mcp/sse", "transport": "sse"})
34
  tools = mcp_client.get_tools()
35
 
 
36
  if openf1_tool_only:
37
  openf1_fn_names = [f"f1_mcp_server_{fn}" for fn in dir(openf1_tools) if callable(getattr(openf1_tools, fn))]
38
  openf1_fn_names.remove("f1_mcp_server_urlopen")
@@ -44,7 +47,6 @@ if __name__ == "__main__":
44
  mcp_client.disconnect()
45
  exit(0)
46
 
47
-
48
  # Define model
49
  if local_model:
50
  model = LiteLLMModel(
@@ -54,20 +56,20 @@ if __name__ == "__main__":
54
  )
55
  else:
56
  model = InferenceClientModel(
57
- model_id="deepseek-ai/DeepSeek-R1",
58
  provider="nebius",
59
  api_key=os.getenv("NEBIUS_API_KEY")
60
  )
61
 
62
  agent = ToolCallingAgent(model=model, tools=[*tools])
63
 
64
-
65
  chat_interface = gr.ChatInterface(
66
  fn=agent_chat,
67
  type="messages",
68
  examples=[
69
- "What are the driver standings for the 2024 Formula 1 season?",
70
- "What is the calendar for the 2024 Formula 1 season?"
71
  ],
72
  title="🏎️ Formula 1 Assistant",
73
  description="This is a simple agent that uses MCP tools to answer questions about Formula 1."
 
7
  # Can manully set this to a specific time to make the agent think it is in the past
8
  time = datetime.datetime.now().astimezone().isoformat()
9
 
10
+ SYSTEM_PROMPT = f"""You are a helpful Formula 1 assistant and strategist. You have access to various F1 data and tools to help answer questions about races, drivers, teams, and more.
11
  Be concise and accurate in your responses. If you don't know something, use the available tools to find the information.
12
  In addition, you will be asked to act as a live race engineer strategist during a Formula 1 race, making crucial calls during the event.
13
+ For formula 1 related tasks, start by calling get_api_endpoints() to see all available endpoints and use them to access the OpenF1 API.
14
+ Then retrieve information about a specific endpoint to make sure it does what you want it to do.
15
+ Lastly, combine the endpoint and filters to create a request to the OpenF1 API and call send_request() to send the request.
16
 
17
  Current time (ISO 8601): {time}"""
18
 
19
 
20
  def agent_chat(message: str, history: list):
21
+ message = f"{SYSTEM_PROMPT}\n\nTask: {message}"
22
+ return agent.run(message)
 
 
23
 
24
 
25
  if __name__ == "__main__":
26
 
27
+ list_tools = False # Set to True to only list tools (used for debugging)
28
+ local_model = False # If you have Ollama installed, set this to True
29
  openf1_tool_only = True
30
 
31
  try:
32
 
33
+ # Connect to my MCP server hosted on HF spaces
34
  mcp_client = MCPClient(
35
  {"url": "https://agents-mcp-hackathon-f1-mcp-server.hf.space/gradio_api/mcp/sse", "transport": "sse"})
36
  tools = mcp_client.get_tools()
37
 
38
+ # Filter tools to only use the OpenF1 library
39
  if openf1_tool_only:
40
  openf1_fn_names = [f"f1_mcp_server_{fn}" for fn in dir(openf1_tools) if callable(getattr(openf1_tools, fn))]
41
  openf1_fn_names.remove("f1_mcp_server_urlopen")
 
47
  mcp_client.disconnect()
48
  exit(0)
49
 
 
50
  # Define model
51
  if local_model:
52
  model = LiteLLMModel(
 
56
  )
57
  else:
58
  model = InferenceClientModel(
59
+ model_id="mistralai/Mistral-Nemo-Instruct-2407",
60
  provider="nebius",
61
  api_key=os.getenv("NEBIUS_API_KEY")
62
  )
63
 
64
  agent = ToolCallingAgent(model=model, tools=[*tools])
65
 
66
+ # Launch chat interface
67
  chat_interface = gr.ChatInterface(
68
  fn=agent_chat,
69
  type="messages",
70
  examples=[
71
+ "What is the calendar for the 2024 Formula 1 season?",
72
+ "Who won the Monaco 2024 GP"
73
  ],
74
  title="🏎️ Formula 1 Assistant",
75
  description="This is a simple agent that uses MCP tools to answer questions about Formula 1."
utils/scripts/inference_test.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from smolagents import InferenceClientModel, LiteLLMModel, ToolCallingAgent, MCPClient, CodeAgent
3
+ from huggingface_hub import InferenceClient
4
+
5
+ # Create the Nebius-backed HuggingFace InferenceClient
6
+ # hf_client = InferenceClient(
7
+ # provider="nebius",
8
+ # api_key=os.getenv("NEBIUS_API_KEY")
9
+ # )
10
+
11
+ # Wrap it for smolagents agentic interface
12
+ model = InferenceClientModel(
13
+ model_id="Qwen/Qwen2.5-VL-72B-Instruct",
14
+ provider="nebius",
15
+ api_key=os.getenv("NEBIUS_API_KEY")
16
+ )
17
+
18
+ messages=[
19
+ {"role": "system", "content": "You are a helpful assistant."},
20
+ {"role": "user", "content": "Tell me an easy ice cream recipe."},
21
+ ]
22
+
23
+ # completion = client.chat.completions.create(
24
+ # model="Qwen/Qwen2.5-VL-72B-Instruct",
25
+ # messages=messages,
26
+ # max_tokens=500
27
+ # )
28
+
29
+ # print(completion.choices[0].message.content)
30
+
31
+ # Example: No tools, just agentic reasoning (tool use can be added if desired)
32
+ agent = ToolCallingAgent(model=model, tools=[])
33
+
34
+ response = agent.run(messages[-1]['content'], max_steps=10)
35
+ print(response)