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,

View file

@ -52,7 +52,6 @@ class ArchHandler:
messages: list[dict],
execution_results: list,
) -> dict:
content = []
for result in execution_results:
content.append(f"<tool_response>\n{json.dumps(result)}\n</tool_response>")

View file

@ -2,16 +2,18 @@ import json
import pytest
from app.arch_fc.arch_fc import process_state
from app.arch_fc.common import ChatMessage, Message
# test process_state
arch_state = '[[{"key":"02ea8ec721b130dc30ec836b79ec675116cd5889bca7d63720bc64baed994fc1","message":{"role":"user","content":"how is the weather in new york?"},"tool_call":{"name":"weather_forecast","arguments":{"city":"new york"}},"tool_response":"{\\"city\\":\\"new york\\",\\"temperature\\":[{\\"date\\":\\"2024-10-07\\",\\"temperature\\":{\\"min\\":68,\\"max\\":79}},{\\"date\\":\\"2024-10-08\\",\\"temperature\\":{\\"min\\":70,\\"max\\":76}},{\\"date\\":\\"2024-10-09\\",\\"temperature\\":{\\"min\\":71,\\"max\\":84}},{\\"date\\":\\"2024-10-10\\",\\"temperature\\":{\\"min\\":61,\\"max\\":79}},{\\"date\\":\\"2024-10-11\\",\\"temperature\\":{\\"min\\":86,\\"max\\":91}},{\\"date\\":\\"2024-10-12\\",\\"temperature\\":{\\"min\\":85,\\"max\\":90}},{\\"date\\":\\"2024-10-13\\",\\"temperature\\":{\\"min\\":72,\\"max\\":89}}],\\"unit\\":\\"F\\"}"}],[{"key":"566b9a2197cba89f35c1e3fbeee55882772ae7627fcf4411dae90282f98a1067","message":{"role":"user","content":"how is the weather in chicago?"},"tool_call":{"name":"weather_forecast","arguments":{"city":"chicago"}},"tool_response":"{\\"city\\":\\"chicago\\",\\"temperature\\":[{\\"date\\":\\"2024-10-07\\",\\"temperature\\":{\\"min\\":54,\\"max\\":64}},{\\"date\\":\\"2024-10-08\\",\\"temperature\\":{\\"min\\":84,\\"max\\":99}},{\\"date\\":\\"2024-10-09\\",\\"temperature\\":{\\"min\\":85,\\"max\\":100}},{\\"date\\":\\"2024-10-10\\",\\"temperature\\":{\\"min\\":50,\\"max\\":62}},{\\"date\\":\\"2024-10-11\\",\\"temperature\\":{\\"min\\":79,\\"max\\":85}},{\\"date\\":\\"2024-10-12\\",\\"temperature\\":{\\"min\\":88,\\"max\\":100}},{\\"date\\":\\"2024-10-13\\",\\"temperature\\":{\\"min\\":56,\\"max\\":61}}],\\"unit\\":\\"F\\"}"}]]'
def test_process_state():
history = []
history.append(Message(role="user", content="how is the weather in new york?"))
history.append(Message(role="user", content="how is the weather in chicago?"))
updated_history = process_state(arch_state, history)
print(json.dumps(updated_history, indent=2))
history = []
history.append(Message(role="user", content="how is the weather in new york?"))
history.append(Message(role="user", content="how is the weather in chicago?"))
updated_history = process_state(arch_state, history)
print(json.dumps(updated_history, indent=2))
if __name__ == "__main__":