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):
class ChatResponse(BaseModel):
reply: str
sources: List[str] = []
sources: List[dict] = []
class ChatArchiveResponse(BaseModel):
......
......@@ -41,7 +41,7 @@ def init_sheet(chat_id: str, messages: list[dict]) -> dict[str, Any]:
"created_at": timestamp,
"updated_at": timestamp,
"history": messages[:],
"retrieval_contexts": [],
"sources": [],
"math_solutions": [],
"LLM_solutions": [],
"decisions": [],
......@@ -84,23 +84,17 @@ def format_sheet(sheet: dict[str, Any]) -> str:
history = format_history(sheet.get("history", []))
parts.append("HISTORY:\n" + (history or "(leer)"))
retrievals = sheet.get("retrieval_contexts", [])
if retrievals:
blocks = []
for item in retrievals:
query = item.get('query', '')
sources = item.get('sources', [])
sources = sheet.get("sources", [])
if sources:
# Erstelle eine formatierte Liste der Quellen mit ihren Scores
source_blocks = []
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))
parts.append(f"SOURCES:\n{source_info}")
else:
parts.append("RETRIEVAL_CONTEXT:\n(leer)")
parts.append("SOURCES:\n(leer)")
math_solutions = sheet.get("math_solutions", [])
if math_solutions:
......@@ -160,24 +154,22 @@ def get_task(sheet: dict[str, Any]) -> str:
def update_retrieval_context(
sheet: dict[str, Any],
query: str,
sources: list[Source],
) -> None:
retrievals = sheet.get("retrieval_contexts", [])
if retrievals:
latest_retrieval = retrievals[-1]
latest_retrieval["sources"] = [source.model_dump()
for source in sources]
latest_retrieval["query"] = query
temp_sources = get_retrieval(sheet)
for source in sources:
temp_sources.append(source)
temp_sources = sorted(sources, key=lambda x: x.score, reverse=True)
temp_sources = temp_sources[:8]
for source in temp_sources:
sheet["sources"].append(source.model_dump())
sheet["updated_at"] = _utc_now()
def get_retrieval(sheet: dict[str, Any]) -> list[Source]:
retrievals = sheet.get("retrieval_contexts", [])
if not retrievals:
sources = sheet.get("sources", [])
if not sources:
return []
latest_retrieval = retrievals[-1]
sources = latest_retrieval.get("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]
def bootstrap_retrieval(sheet: dict, query_text: str, tool_log: list[dict]) -> None:
sources = retrieve_context(query_text=query_text)
retrievals = sheet.get("retrieval_contexts", [])
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",
{"query": query_text}, source_dump)
......
......@@ -15,7 +15,7 @@ def _on_bootstrap(state: base.ChatState, query_text: str) -> 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)
if state.new_chat:
......@@ -35,12 +35,12 @@ def _on_turn_logic(state: base.ChatState) -> None:
context_store.add_decision(state.sheet, decision)
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)
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 = {
"query": state.last_user if not state.new_chat else None,
"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