-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_mcp.py
More file actions
87 lines (80 loc) · 3.3 KB
/
model_mcp.py
File metadata and controls
87 lines (80 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import requests
import json
from nace_navigator import NACENavigator
import logging
logger = logging.getLogger(__name__)
class MCPModel():
'''Class managing the conversation with the llm '''
def __init__(self, navigator, init_mess=''):
self.navigator: NACENavigator = navigator
self.model = "gpt-oss:20b"
self.token = os.environ["GPT_OS_KEY"]
self.url = 'https://llm.lab.sspcloud.fr/api/chat/completions'
self.headers = {
'Authorization': f'Bearer {self.token}',
'Content-Type': 'application/json'
}
self.tools = [
{
"type": "function",
"function": {
"name": "get_node_info",
"description": "Get details of the current node.",
"parameters": {
"type": "object",
"properties": {
"child_code": {"type": "string"}
},
"required": [],
},
},
},
{
"type": "function",
"function": {
"name": "go_down",
"description": "Move to a child node by code.",
"parameters": {
"type": "object",
"properties": {
"child_code": {"type": "string"}
},
"required": ["child_code"],
},
},
},
{
"type": "function",
"function": {
"name": "go_up",
"description": "Move to the parent node.",
"parameters": {"type": "object", "properties": {}},
},
},
]
def request_llm(self, messages) -> json:
logger.info(f"Messages sent : {messages[-1]} \n")
data = {
"model": self.model,
"messages": messages,
"tools": self.tools,
"tool_choice": "any",
"parallel_tool_calls": False,
}
response = requests.post(self.url, headers=self.headers, json=data)
logger.info(f"Response : {response.json()['choices']} \n")
return response.json()
def use_tool(self, call) -> str:
args = json.loads(call['function']['arguments'] or "{}")
logger.info
name = call['f²args} arguments")
if name == "get_node_info":
result = self.navigator.get_node_info(*args)
elif name == "go_down":
result = self.navigator.go_down(*args)
elif name == "go_up":
result = self.navigator.go_up()
else:
result = "Error: unknown tool"
return result