Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
math_tutor_dev
public_math_tutor
Commits
4c15ecde
Commit
4c15ecde
authored
Jan 28, 2026
by
Kantz
Browse files
änderung des retrivals
parent
33972126
Changes
4
Hide whitespace changes
Inline
Side-by-side
math-tutor/backend/app/LLM_services/decision_LLM.py
View file @
4c15ecde
...
@@ -13,16 +13,16 @@ CLASSIFIER_PROMPT = (
...
@@ -13,16 +13,16 @@ CLASSIFIER_PROMPT = (
def
needs_more_context
(
history
:
str
,
context_sheet
:
str
)
->
dict
:
def
needs_more_context
(
history
:
str
,
context_sheet
:
str
)
->
dict
:
prompt
=
(
prompt
=
(
"Historie:
\n
"
"Kontextblatt:
\n
"
+
history
+
"
\n\n
Kontextblatt:
\n
"
+
context_sheet
+
context_sheet
+
"
\n\n
Historie:
\n
"
+
history
+
"
\n\n
Antwortformat: JSON."
+
"
\n\n
Antwortformat: JSON."
)
)
result
=
llm_client
.
chat
(
result
=
llm_client
.
chat
(
messages
=
[
messages
=
[
{
"role"
:
"system"
,
"content"
:
CLASSIFIER_PROMPT
},
{
"role"
:
"user"
,
"content"
:
prompt
},
{
"role"
:
"user"
,
"content"
:
prompt
},
{
"role"
:
"system"
,
"content"
:
CLASSIFIER_PROMPT
},
],
],
use_ollama
=
True
,
use_ollama
=
True
,
)
)
...
...
math-tutor/backend/app/deterministic_services/context_store.py
View file @
4c15ecde
...
@@ -80,14 +80,28 @@ def add_retrieval_context(
...
@@ -80,14 +80,28 @@ def add_retrieval_context(
sheet
:
dict
[
str
,
Any
],
sheet
:
dict
[
str
,
Any
],
query
:
str
,
query
:
str
,
context
:
str
,
context
:
str
,
sources
:
list
[
str
],
sources
:
list
[
dict
],
)
->
None
:
)
->
None
:
sheet
[
"retrieval_contexts"
].
append
(
retrieval_entry
=
{
"query"
:
query
,
"context"
:
context
,
"sources"
:
sources
}
{
"query"
:
query
,
"context"
:
context
,
"sources"
:
sources
}
sheet
[
"retrieval_contexts"
].
append
(
retrieval_entry
)
)
sheet
[
"updated_at"
]
=
_utc_now
()
sheet
[
"updated_at"
]
=
_utc_now
()
def
update_retrieval_context
(
sheet
:
dict
[
str
,
Any
],
query
:
str
,
context
:
str
,
sources
:
list
[
dict
],
)
->
None
:
retrievals
=
sheet
.
get
(
"retrieval_contexts"
,
[])
if
retrievals
:
latest_retrieval
=
retrievals
[
-
1
]
latest_retrieval
[
"context"
]
=
context
latest_retrieval
[
"sources"
]
=
sources
latest_retrieval
[
"query"
]
=
query
sheet
[
"updated_at"
]
=
_utc_now
()
def
add_math_solution
(
def
add_math_solution
(
sheet
:
dict
[
str
,
Any
],
sheet
:
dict
[
str
,
Any
],
task
:
str
,
task
:
str
,
...
@@ -145,7 +159,17 @@ def format_sheet(sheet: dict[str, Any]) -> str:
...
@@ -145,7 +159,17 @@ def format_sheet(sheet: dict[str, Any]) -> str:
if
retrievals
:
if
retrievals
:
blocks
=
[]
blocks
=
[]
for
item
in
retrievals
:
for
item
in
retrievals
:
blocks
.
append
(
f
"QUERY:
{
item
.
get
(
'query'
,
''
)
}
\n
{
item
.
get
(
'context'
,
''
)
}
"
)
query
=
item
.
get
(
'query'
,
''
)
context
=
item
.
get
(
'context'
,
''
)
sources
=
item
.
get
(
'sources'
,
[])
# Erstelle eine formatierte Liste der Quellen mit ihren Scores
source_blocks
=
[]
for
source
in
sources
:
source_blocks
.
append
(
f
" - [
{
source
.
get
(
'score'
,
0
)
:
.
3
f
}
]
{
source
.
get
(
'label'
,
''
)
}
{
source
.
get
(
'ref'
,
''
)
}
"
)
source_info
=
"
\n
"
.
join
(
source_blocks
)
if
source_blocks
else
""
blocks
.
append
(
f
"QUERY:
{
query
}
\n
{
context
}
\n
SOURCES:
\n
{
source_info
}
"
)
parts
.
append
(
"RETRIEVAL_CONTEXT:
\n
"
+
"
\n\n
"
.
join
(
blocks
))
parts
.
append
(
"RETRIEVAL_CONTEXT:
\n
"
+
"
\n\n
"
.
join
(
blocks
))
else
:
else
:
parts
.
append
(
"RETRIEVAL_CONTEXT:
\n
(leer)"
)
parts
.
append
(
"RETRIEVAL_CONTEXT:
\n
(leer)"
)
...
...
math-tutor/backend/app/deterministic_services/orchestrator.py
View file @
4c15ecde
from
app.deterministic_services
import
context_store
,
retrieval_service
,
tool_logging
from
app.deterministic_services
import
context_store
,
retrieval_service
,
tool_logging
from
app.tools
import
math_tool
from
app.tools
import
math_tool
from
app.LLM_services
import
hint_LLM
,
decision_LLM
,
math_intent_LLM
,
solver_LLM
from
app.LLM_services
import
hint_LLM
,
decision_LLM
,
math_intent_LLM
,
solver_LLM
from
typing
import
List
def
_append_tool_log
(
tool_log
:
list
[
dict
],
name
:
str
,
args
:
dict
,
response
:
object
)
->
None
:
def
_append_tool_log
(
tool_log
:
list
[
dict
],
name
:
str
,
args
:
dict
,
response
:
object
)
->
None
:
...
@@ -8,10 +9,17 @@ def _append_tool_log(tool_log: list[dict], name: str, args: dict, response: obje
...
@@ -8,10 +9,17 @@ def _append_tool_log(tool_log: list[dict], name: str, args: dict, response: obje
def
_bootstrap_context
(
sheet
:
dict
,
query_text
:
str
,
tool_log
:
list
[
dict
])
->
None
:
def
_bootstrap_context
(
sheet
:
dict
,
query_text
:
str
,
tool_log
:
list
[
dict
])
->
None
:
# Erstelle einen neuen Retrieval-Block oder aktualisiere den bestehenden
context
,
sources
=
retrieval_service
.
retrieve_context
(
query_text
=
query_text
)
context
,
sources
=
retrieval_service
.
retrieve_context
(
query_text
=
query_text
)
context_store
.
add_retrieval_context
(
sheet
,
query_text
,
context
,
sources
)
_append_tool_log
(
tool_log
,
"retrieve_context"
,
{
"query"
:
query_text
},
{
"context"
:
context
,
"sources"
:
sources
})
retrievals
=
sheet
.
get
(
"retrieval_contexts"
,
[])
if
retrievals
:
context_store
.
update_retrieval_context
(
sheet
,
query_text
,
context
,
sources
)
_append_tool_log
(
tool_log
,
"update_retrieve_context"
,
{
"query"
:
query_text
},
{
"context"
:
context
,
"sources"
:
sources
})
else
:
context_store
.
add_retrieval_context
(
sheet
,
query_text
,
context
,
sources
)
_append_tool_log
(
tool_log
,
"retrieve_context"
,
{
"query"
:
query_text
},
{
"context"
:
context
,
"sources"
:
sources
})
math_request
=
math_intent_LLM
.
extract_math_request
(
query_text
)
math_request
=
math_intent_LLM
.
extract_math_request
(
query_text
)
if
math_request
:
if
math_request
:
solution
=
math_tool
.
sympy_solve
(
**
math_request
)
solution
=
math_tool
.
sympy_solve
(
**
math_request
)
...
@@ -73,8 +81,8 @@ def run_chat(messages: list[dict], draft: str | None = None) -> dict:
...
@@ -73,8 +81,8 @@ def run_chat(messages: list[dict], draft: str | None = None) -> dict:
context_store
.
add_decision
(
sheet
,
decision
)
context_store
.
add_decision
(
sheet
,
decision
)
if
decision
.
get
(
"needs_more_context"
):
if
decision
.
get
(
"needs_more_context"
):
_bootstrap_context
(
sheet
,
(
"
\n
"
).
join
(
_extract_user_messages
(
messages
))
,
tool_log
)
full_query
=
(
"
\n
"
).
join
(
_extract_user_messages
(
messages
))
sheet_text
=
context_store
.
format_sheet
(
sheet
)
_bootstrap_context
(
sheet
,
full_query
,
tool_log
)
# Hinweis und Ausgaben generierung
# Hinweis und Ausgaben generierung
hint_args
=
{
hint_args
=
{
...
...
math-tutor/backend/app/deterministic_services/retrieval_service.py
View file @
4c15ecde
...
@@ -54,19 +54,24 @@ def _append_group(
...
@@ -54,19 +54,24 @@ def _append_group(
items
:
List
[
vector_store
.
Retrieved
],
items
:
List
[
vector_store
.
Retrieved
],
limit
:
int
,
limit
:
int
,
blocks
:
List
[
str
],
blocks
:
List
[
str
],
sources
:
List
[
str
],
sources
:
List
[
dict
],
)
->
None
:
)
->
None
:
if
not
items
:
if
not
items
:
return
return
for
doc
in
items
[:
limit
]:
for
doc
in
items
[:
limit
]:
ref
=
_format_ref
(
doc
)
ref
=
_format_ref
(
doc
)
blocks
.
append
(
f
"
{
label
}
{
ref
}
\n
{
doc
.
markdown
}
"
)
blocks
.
append
(
f
"
{
label
}
{
ref
}
\n
{
doc
.
markdown
}
"
)
sources
.
append
(
ref
)
sources
.
append
({
"ref"
:
ref
,
"context"
:
doc
.
markdown
,
"score"
:
doc
.
score
,
"label"
:
label
})
def
build_context
(
result
:
dict
)
->
tuple
[
str
,
List
[
str
]]:
def
build_context
(
result
:
dict
)
->
tuple
[
str
,
List
[
dict
]]:
blocks
:
List
[
str
]
=
[]
blocks
:
List
[
str
]
=
[]
sources
:
List
[
str
]
=
[]
sources
:
List
[
dict
]
=
[]
_append_group
(
"DIRECT"
,
result
.
get
(
"children_direct"
,
[]),
CONTEXT_LIMITS
[
"direct"
],
blocks
,
sources
)
_append_group
(
"DIRECT"
,
result
.
get
(
"children_direct"
,
[]),
CONTEXT_LIMITS
[
"direct"
],
blocks
,
sources
)
_append_group
(
"INDIRECT"
,
result
.
get
(
"children_expanded"
,
[]),
CONTEXT_LIMITS
[
"indirect"
],
blocks
,
sources
)
_append_group
(
"INDIRECT"
,
result
.
get
(
"children_expanded"
,
[]),
CONTEXT_LIMITS
[
"indirect"
],
blocks
,
sources
)
...
@@ -78,7 +83,7 @@ def build_context(result: dict) -> tuple[str, List[str]]:
...
@@ -78,7 +83,7 @@ def build_context(result: dict) -> tuple[str, List[str]]:
return
"KONTEXT:
\n
"
+
"
\n\n
"
.
join
(
blocks
),
sources
return
"KONTEXT:
\n
"
+
"
\n\n
"
.
join
(
blocks
),
sources
def
retrieve_context
(
query_text
:
str
,
pg_url
:
str
|
None
=
None
)
->
tuple
[
str
,
List
[
str
]]:
def
retrieve_context
(
query_text
:
str
,
pg_url
:
str
|
None
=
None
)
->
tuple
[
str
,
List
[
dict
]]:
url
=
pg_url
or
config
.
get_postgres_url
()
url
=
pg_url
or
config
.
get_postgres_url
()
embedder
=
_get_embedder
()
embedder
=
_get_embedder
()
retrieval
=
vector_store
.
retrieve
(
retrieval
=
vector_store
.
retrieve
(
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment