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

@ -1182,7 +1182,7 @@ CHUNK_OVERLAP = PersistentConfig(
) )
DEFAULT_RAG_TEMPLATE = """### Task: DEFAULT_RAG_TEMPLATE = """### Task:
Respond to the user query using the provided context, incorporating inline citations in the format [source_id]. Respond to the user query using the provided context, incorporating inline citations in the format [source_id] **only when the <source_id> tag is explicitly provided** in the context.
### Guidelines: ### Guidelines:
- If you don't know the answer, clearly state that. - If you don't know the answer, clearly state that.
@ -1190,16 +1190,18 @@ Respond to the user query using the provided context, incorporating inline citat
- Respond in the same language as the user's query. - Respond in the same language as the user's query.
- If the context is unreadable or of poor quality, inform the user and provide the best possible answer. - If the context is unreadable or of poor quality, inform the user and provide the best possible answer.
- If the answer isn't present in the context but you possess the knowledge, explain this to the user and provide the answer using your own understanding. - If the answer isn't present in the context but you possess the knowledge, explain this to the user and provide the answer using your own understanding.
- Include inline citations using [source_id] corresponding to the sources listed in the context. - **Only include inline citations using [source_id] when a <source_id> tag is explicitly provided in the context.**
- Do not cite if the <source_id> tag is not provided in the context.
- Do not use XML tags in your response. - Do not use XML tags in your response.
- Ensure citations are concise and directly related to the information provided. - Ensure citations are concise and directly related to the information provided.
### Example of Citation: ### Example of Citation:
If the user asks about a specific topic and the information is found in "whitepaper.pdf", the response should include the citation like so: If the user asks about a specific topic and the information is found in "whitepaper.pdf" with a provided <source_id>, the response should include the citation like so:
* "According to the study, the proposed method increases efficiency by 20% [whitepaper.pdf]." * "According to the study, the proposed method increases efficiency by 20% [whitepaper.pdf]."
If no <source_id> is present, the response should omit the citation.
### Output: ### Output:
Provide a clear and direct response to the user's query, including inline citations in the format [source_id] where relevant. Provide a clear and direct response to the user's query, including inline citations in the format [source_id] only when the <source_id> tag is present in the context.
<context> <context>
{{CONTEXT}} {{CONTEXT}}

View File

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

View File

@ -43,6 +43,10 @@
$: { $: {
_citations = citations.reduce((acc, citation) => { _citations = citations.reduce((acc, citation) => {
if (Object.keys(citation).length === 0) {
return acc;
}
citation.document.forEach((document, index) => { citation.document.forEach((document, index) => {
const metadata = citation.metadata?.[index]; const metadata = citation.metadata?.[index];
const distance = citation.distances?.[index]; const distance = citation.distances?.[index];