formating and mointoring change (#136)

This commit is contained in:
Co Tran 2024-10-07 15:21:05 -07:00 committed by GitHub
parent 976b2eaae0
commit 93abe553e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 83 additions and 47 deletions

View file

@ -50,15 +50,16 @@ logger.info(f"serving mode: {mode}")
logger.info(f"using model: {chosen_model}")
logger.info(f"using endpoint: {endpoint}")
def process_state(arch_state, history: list[Message]):
print("state: {}".format(arch_state))
state_json = json.loads(arch_state)
state_map = {}
if state_json:
for tools_state in state_json:
for tool_state in tools_state:
state_map[tool_state['key']] = tool_state
for tools_state in state_json:
for tool_state in tools_state:
state_map[tool_state["key"]] = tool_state
print(f"state_map: {json.dumps(state_map)}")
@ -66,27 +67,38 @@ def process_state(arch_state, history: list[Message]):
updated_history = []
for hist in history:
updated_history.append({"role": hist.role, "content": hist.content})
if hist.role == 'user':
if hist.role == "user":
sha_history.append(hist.content)
sha256_hash = hashlib.sha256()
joined_key_str = ('#.#').join(sha_history)
joined_key_str = ("#.#").join(sha_history)
sha256_hash.update(joined_key_str.encode())
sha_key = sha256_hash.hexdigest()
print(f"sha_key: {sha_key}")
if sha_key in state_map:
tool_call_state = state_map[sha_key]
if 'tool_call' in tool_call_state:
tool_call_str = json.dumps(tool_call_state['tool_call'])
updated_history.append({"role": "assistant", "content": f"<tool_call>\n{tool_call_str}\n</tool_call>"})
if 'tool_response' in tool_call_state:
tool_resp = tool_call_state['tool_response']
#TODO: try with role = user as well
updated_history.append({"role": "user", "content": f"<tool_response>\n{tool_resp}\n</tool_response>"})
if "tool_call" in tool_call_state:
tool_call_str = json.dumps(tool_call_state["tool_call"])
updated_history.append(
{
"role": "assistant",
"content": f"<tool_call>\n{tool_call_str}\n</tool_call>",
}
)
if "tool_response" in tool_call_state:
tool_resp = tool_call_state["tool_response"]
# TODO: try with role = user as well
updated_history.append(
{
"role": "user",
"content": f"<tool_response>\n{tool_resp}\n</tool_response>",
}
)
# we dont want to match this state with any other messages
del(state_map[sha_key])
del state_map[sha_key]
return updated_history
async def chat_completion(req: ChatMessage, res: Response):
logger.info("starting request")
tools_encoded = handler._format_system(req.tools)
@ -98,7 +110,9 @@ async def chat_completion(req: ChatMessage, res: Response):
for message in updated_history:
messages.append({"role": message["role"], "content": message["content"]})
logger.info(f"model_server => arch_fc: {chosen_model}, messages: {json.dumps(messages)}")
logger.info(
f"model_server => arch_fc: {chosen_model}, messages: {json.dumps(messages)}"
)
completions_params = params["params"]
resp = client.chat.completions.create(
messages=messages,