refac: inline citations

This commit is contained in:
Timothy Jaeryang Baek
2024-11-21 18:26:38 -08:00
parent 714ed248fb
commit 7062e637e8
3 changed files with 24 additions and 6 deletions

View File

@@ -463,6 +463,8 @@ async def chat_completion_tools_handler(
except Exception as e:
tool_output = str(e)
print(tools[tool_function_name]["citation"])
if tools[tool_function_name]["citation"]:
citations.append(
{
@@ -473,6 +475,9 @@ async def chat_completion_tools_handler(
"metadata": [{"source": tool_function_name}],
}
)
else:
citations.append({})
if tools[tool_function_name]["file_handler"]:
skip_files = True
@@ -485,7 +490,7 @@ async def chat_completion_tools_handler(
log.exception(f"Error: {e}")
content = None
log.debug(f"tool_contexts: {contexts}")
log.debug(f"tool_contexts: {contexts} {citations}")
if skip_files and "files" in body.get("metadata", {}):
del body["metadata"]["files"]
@@ -683,7 +688,14 @@ class ChatCompletionMiddleware(BaseHTTPMiddleware):
for context_idx, context in enumerate(contexts):
print(context)
source_id = citations[context_idx].get("source", {}).get("name", "")
context_string += f"<source><source_id>{source_id}</source_id><source_context>{context}</source_context></source>\n"
print(f"\n\n\n\n{source_id}\n\n\n\n")
if source_id:
context_string += f"<source><source_id>{source_id}</source_id><source_context>{context}</source_context></source>\n"
else:
context_string += (
f"<source><source_context>{context}</source_context></source>\n"
)
context_string = context_string.strip()
prompt = get_last_user_message(body["messages"])