-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi_interface.py
153 lines (134 loc) · 5.61 KB
/
api_interface.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import prance
from typing import List
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
from mistral_common.protocol.instruct.request import ChatCompletionRequest
from mistral_common.protocol.instruct.tool_calls import Function, Tool
from mistral_common.protocol.instruct.messages import UserMessage
import json
import requests
import functools
import os
def generate_tools(objs, function_end_point)-> List[Tool]:
params = ['operationId', 'description', 'parameters']
parser = prance.ResolvingParser(function_end_point, backend='openapi-spec-validator')
spec = parser.specification
user_tools = []
for obj in objs:
resource, field = obj
path = '/' + resource + '/{' + field + '}'
function_name=spec['paths'][path]['get'][params[0]]
function_description=spec['paths'][path]['get'][params[1]]
function_parameters=spec['paths'][path]['get'][params[2]]
func_parameters = {
"type": "object",
"properties": {
function_parameters[0]['name']: {
"type": function_parameters[0]['schema']['type'],
"description": function_parameters[0]['description']
}
},
"required": [function_parameters[0]['name']]
}
user_function= Function(name = function_name, description = function_description, parameters = func_parameters, )
user_tool = Tool(function = user_function)
user_tools.append(user_tool)
return user_tools
def get_user_messages(queries: List[str]) -> List[UserMessage]:
user_messages=[]
for query in queries:
user_message = UserMessage(content=query)
user_messages.append(user_message)
return user_messages
def process_results(result, messages):
#print(result)
result_format = result['response'].split("\n\n")
#print(result_format)
result_tool_calls = result_format[0].replace("[TOOL_CALLS] ","")
tool_calls = json.loads(result_tool_calls)
index = 0
for tool_call in tool_calls:
try:
function_name = tool_call["name"]
function_params = (tool_call["arguments"])
print(messages[index].content)
function_result = names_to_functions[function_name](**function_params)
print(function_result)
index = index + 1
except:
print(function_name + " is not defined")
continue
query_result = "The answer is - "
return query_result
def getPetById(petId: int) -> str:
try:
method = 'GET'
headers=None
data=None
url = 'https://petstore3.swagger.io/api/v3/pet/' + str(petId)
response = requests.request(method, url, headers=headers, data=data)
# Raise an exception if the response was unsuccessful
response.raise_for_status()
#response = make_api_call('GET', url + str(petId))
if response.ok :
json_response = response.json()
if petId == json_response['id']:
return json_response
return json.dumps({'error': 'Pet id not found.'})
except requests.exceptions.HTTPError as e:
if response.status_code == 404:
return json.dumps({'error': 'Pet id not found.'})
else:
return json.dumps({'error': 'Error with API.'})
def getUserByName(username: str) -> str:
try:
url = 'https://petstore3.swagger.io/api/v3/user/' + username
response = requests.get(url)
# Raise an exception if the response was unsuccessful
response.raise_for_status()
if response.ok :
json_response = response.json()
if username == json_response['username']:
return json_response
return json.dumps({'error': 'Username id not found.'})
except requests.exceptions.HTTPError as e:
if response.status_code == 404:
return json.dumps({'error': 'Username not found.'})
else:
return json.dumps({'error': 'Error with API.'})
names_to_functions = {
'getPetById': functools.partial(getPetById, petId=''),
'getUserByName': functools.partial(getUserByName, username='')
}
def execute_generator(queries, return_objs):
function_end_point = 'https://petstore3.swagger.io/api/v3/openapi.json'
user_messages=get_user_messages(queries)
user_tools = generate_tools(return_objs, function_end_point)
tokenizer = MistralTokenizer.v3()
completion_request = ChatCompletionRequest(tools=user_tools, messages=user_messages,)
tokenized = tokenizer.encode_chat_completion(completion_request)
_, text = tokenized.tokens, tokenized.text
#print(text)
ollama_endpoint_env = os.environ.get('OLLAMA_ENDPOINT')
model = "mistral:7b"
prompt = text
if ollama_endpoint_env is None:
ollama_endpoint_env = 'http://localhost:11434'
ollama_endpoint = ollama_endpoint_env + "/api/generate" # replace with localhost
response = requests.post(ollama_endpoint,
json={
'model': model,
'prompt': prompt,
'stream':False,
'raw': True
}, stream=False
)
response.raise_for_status()
result = response.json()
query_result = process_results(result, user_messages)
return query_result
def main():
queries = ["What's the status of my Pet 1?", "Find information of user user1?" , "What's the status of my Store Order 3?"]
return_objs = [['pet','petId'], ['user', 'username'], ['store/order','orderId']]
execute_generator(queries, return_objs)
if __name__ == "__main__":
main()