Commit c4b730a9 authored by Kantz's avatar Kantz
Browse files

retrvial ungebaut auf Sources und weitergabe repariert

parent 9002abc9
...@@ -33,7 +33,7 @@ class ChatRequest(BaseModel): ...@@ -33,7 +33,7 @@ class ChatRequest(BaseModel):
class ChatResponse(BaseModel): class ChatResponse(BaseModel):
reply: str reply: str
sources: List[str] = [] sources: List[dict] = []
class ChatArchiveResponse(BaseModel): class ChatArchiveResponse(BaseModel):
......
...@@ -41,7 +41,7 @@ def init_sheet(chat_id: str, messages: list[dict]) -> dict[str, Any]: ...@@ -41,7 +41,7 @@ def init_sheet(chat_id: str, messages: list[dict]) -> dict[str, Any]:
"created_at": timestamp, "created_at": timestamp,
"updated_at": timestamp, "updated_at": timestamp,
"history": messages[:], "history": messages[:],
"retrieval_contexts": [], "sources": [],
"math_solutions": [], "math_solutions": [],
"LLM_solutions": [], "LLM_solutions": [],
"decisions": [], "decisions": [],
...@@ -84,23 +84,17 @@ def format_sheet(sheet: dict[str, Any]) -> str: ...@@ -84,23 +84,17 @@ def format_sheet(sheet: dict[str, Any]) -> str:
history = format_history(sheet.get("history", [])) history = format_history(sheet.get("history", []))
parts.append("HISTORY:\n" + (history or "(leer)")) parts.append("HISTORY:\n" + (history or "(leer)"))
retrievals = sheet.get("retrieval_contexts", []) sources = sheet.get("sources", [])
if retrievals: if sources:
blocks = [] # Erstelle eine formatierte Liste der Quellen mit ihren Scores
for item in retrievals: source_blocks = []
query = item.get('query', '') for source in sources:
sources = item.get('sources', []) source_blocks.append(Source.model_validate(source).to_string())
# Erstelle eine formatierte Liste der Quellen mit ihren Scores source_info = "\n".join(source_blocks) if source_blocks else ""
source_blocks = [] parts.append(f"SOURCES:\n{source_info}")
for source in sources:
source_blocks.append(Source.model_validate(source).to_string())
source_info = "\n".join(source_blocks) if source_blocks else ""
blocks.append(f"QUERY: {query}\nSOURCES:\n{source_info}")
parts.append("RETRIEVAL_CONTEXT:\n" + "\n\n".join(blocks))
else: else:
parts.append("RETRIEVAL_CONTEXT:\n(leer)") parts.append("SOURCES:\n(leer)")
math_solutions = sheet.get("math_solutions", []) math_solutions = sheet.get("math_solutions", [])
if math_solutions: if math_solutions:
...@@ -160,24 +154,22 @@ def get_task(sheet: dict[str, Any]) -> str: ...@@ -160,24 +154,22 @@ def get_task(sheet: dict[str, Any]) -> str:
def update_retrieval_context( def update_retrieval_context(
sheet: dict[str, Any], sheet: dict[str, Any],
query: str,
sources: list[Source], sources: list[Source],
) -> None: ) -> None:
retrievals = sheet.get("retrieval_contexts", []) temp_sources = get_retrieval(sheet)
if retrievals: for source in sources:
latest_retrieval = retrievals[-1] temp_sources.append(source)
latest_retrieval["sources"] = [source.model_dump() temp_sources = sorted(sources, key=lambda x: x.score, reverse=True)
for source in sources] temp_sources = temp_sources[:8]
latest_retrieval["query"] = query for source in temp_sources:
sheet["updated_at"] = _utc_now() sheet["sources"].append(source.model_dump())
sheet["updated_at"] = _utc_now()
def get_retrieval(sheet: dict[str, Any]) -> list[Source]: def get_retrieval(sheet: dict[str, Any]) -> list[Source]:
retrievals = sheet.get("retrieval_contexts", []) sources = sheet.get("sources", [])
if not retrievals: if not sources:
return [] return []
latest_retrieval = retrievals[-1]
sources = latest_retrieval.get("sources", [])
return [Source.model_validate(source) for source in sources] return [Source.model_validate(source) for source in sources]
# --------------------------------------------------------------------------------------------------- # ---------------------------------------------------------------------------------------------------
......
...@@ -57,10 +57,9 @@ def retrieve_context(query_text: str, pg_url: str | None = None) -> List[Source] ...@@ -57,10 +57,9 @@ def retrieve_context(query_text: str, pg_url: str | None = None) -> List[Source]
def bootstrap_retrieval(sheet: dict, query_text: str, tool_log: list[dict]) -> None: def bootstrap_retrieval(sheet: dict, query_text: str, tool_log: list[dict]) -> None:
sources = retrieve_context(query_text=query_text) sources = retrieve_context(query_text=query_text)
retrievals = sheet.get("retrieval_contexts", [])
source_dump = {"sources": [source.to_string() for source in sources]} source_dump = {"sources": [source.to_string() for source in sources]}
context_store.update_retrieval_context(sheet, query_text, sources) context_store.update_retrieval_context(sheet, sources)
append_tool_log(tool_log, "update_retrieve_context", append_tool_log(tool_log, "update_retrieve_context",
{"query": query_text}, source_dump) {"query": query_text}, source_dump)
......
...@@ -15,7 +15,7 @@ def _on_bootstrap(state: base.ChatState, query_text: str) -> None: ...@@ -15,7 +15,7 @@ def _on_bootstrap(state: base.ChatState, query_text: str) -> None:
def _on_turn_logic(state: base.ChatState) -> None: def _on_turn_logic(state: base.ChatState) -> None:
history_turns = context_store.history_turns(state.messages) history_turns = context_store.get_history_turns(state.sheet)
sheet_text = context_store.format_sheet(state.sheet) sheet_text = context_store.format_sheet(state.sheet)
if state.new_chat: if state.new_chat:
...@@ -35,12 +35,12 @@ def _on_turn_logic(state: base.ChatState) -> None: ...@@ -35,12 +35,12 @@ def _on_turn_logic(state: base.ChatState) -> None:
context_store.add_decision(state.sheet, decision) context_store.add_decision(state.sheet, decision)
if decision.get("needs_more_context"): if decision.get("needs_more_context"):
full_query = "\n".join(base.extract_user_messages(state.messages)) full_query = "\n".join(base.extract_user_messages(state.sheet))
_on_bootstrap(state, full_query) _on_bootstrap(state, full_query)
def _on_build_reply(state: base.ChatState) -> str | None: def _on_build_reply(state: base.ChatState) -> str | None:
history_turns = context_store.history_turns(state.messages) history_turns = context_store.get_history_turns(state.sheet)
args = { args = {
"query": state.last_user if not state.new_chat else None, "query": state.last_user if not state.new_chat else None,
"task": context_store.get_task(state.sheet), "task": context_store.get_task(state.sheet),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment