-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtogether.py
104 lines (88 loc) · 3.62 KB
/
together.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
import os
import time
from bfcl.model_handler.model_style import ModelStyle
from bfcl.model_handler.api_inference.openai import OpenAIHandler
from openai import OpenAI
class TogetherHandler(OpenAIHandler):
def __init__(self, model_name, temperature) -> None:
super().__init__(model_name, temperature)
self.model_style = ModelStyle.TOGETHER_AI
self.client = OpenAI(
base_url="https://api.together.xyz/v1",
api_key=os.getenv("TOGETHER_API_KEY"),
)
#### FC methods ####
def _query_FC(self, inference_data: dict):
message: list[dict] = inference_data["message"]
tools = inference_data["tools"]
inference_data["inference_input_log"] = {"message": message, "tools": tools}
start_time = time.time()
if len(tools) > 0:
api_response = self.client.chat.completions.create(
messages=message,
model=self.model_name,
temperature=self.temperature,
tools=tools,
)
else:
api_response = self.client.chat.completions.create(
messages=message,
model=self.model_name,
temperature=self.temperature,
)
end_time = time.time()
return api_response, end_time - start_time
def _pre_query_processing_FC(self, inference_data: dict, test_entry: dict) -> dict:
return super()._pre_query_processing_FC(inference_data, test_entry)
def _compile_tools(self, inference_data: dict, test_entry: dict) -> dict:
return super()._compile_tools(inference_data, test_entry)
def _parse_query_response_FC(self, api_response: any) -> dict:
try:
model_responses = [
{func_call.function.name: func_call.function.arguments}
for func_call in api_response.choices[0].message.tool_calls
]
tool_calls = [
tool_call.model_dump()
for tool_call in api_response.choices[0].message.tool_calls
]
except:
model_responses = api_response.choices[0].message.content
tool_calls = []
return {
"model_responses": model_responses,
"model_responses_message_for_chat_history": tool_calls,
"input_token": api_response.usage.prompt_tokens,
"output_token": api_response.usage.completion_tokens,
}
def add_first_turn_message_FC(
self, inference_data: dict, first_turn_message: list[dict]
) -> dict:
return super().add_first_turn_message_FC(inference_data, first_turn_message)
def _add_next_turn_user_message_FC(
self, inference_data: dict, user_message: list[dict]
) -> dict:
return super()._add_next_turn_user_message_FC(inference_data, user_message)
def _add_assistant_message_FC(
self, inference_data: dict, model_response_data: dict
) -> dict:
inference_data["message"].append(
{
"role": "assistant",
"content": "",
"tool_calls": model_response_data[
"model_responses_message_for_chat_history"
],
}
)
return inference_data
def _add_execution_results_FC(
self, inference_data: dict, execution_results: list[str], model_response_data: dict
) -> dict:
for execution_result in execution_results:
tool_message = {
"role": "tool",
"content": execution_result,
}
inference_data["message"].append(tool_message)
return inference_data