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
e02dd127
Commit
e02dd127
authored
Feb 04, 2026
by
Kantz
Browse files
context API hinzugefügt
parent
90e48a24
Changes
2
Show whitespace changes
Inline
Side-by-side
math-tutor/backend/app/api/context.py
0 → 100644
View file @
e02dd127
from
__future__
import
annotations
from
typing
import
List
,
Optional
from
fastapi
import
APIRouter
,
HTTPException
from
pydantic
import
BaseModel
,
Field
from
app.deterministic_services
import
context_store
from
app.deterministic_services.vector_store
import
Retrieved
router
=
APIRouter
()
class
ChatMessage
(
BaseModel
):
role
:
str
=
Field
(...,
pattern
=
"^(user|assistant)$"
)
text
:
str
=
Field
(...,
min_length
=
1
)
class
ContextRequest
(
BaseModel
):
messages
:
List
[
ChatMessage
]
draft
:
Optional
[
str
]
=
None
@
router
.
post
(
"/api/context/retrieval"
)
def
get_retrieval_context
(
request
:
ContextRequest
)
->
dict
:
if
not
request
or
not
request
.
messages
:
raise
HTTPException
(
status_code
=
400
,
detail
=
"messages required"
)
messages
=
[{
"role"
:
m
.
role
,
"content"
:
m
.
text
}
for
m
in
request
.
messages
]
chat_id
=
context_store
.
get_chat_id
(
messages
,
draft
=
request
.
draft
)
sheet
=
context_store
.
load_sheet
(
chat_id
)
if
not
sheet
:
raise
HTTPException
(
status_code
=
404
,
detail
=
"context sheet not found"
)
result
=
context_store
.
get_retrieval_context
(
sheet
)
if
not
result
or
not
result
.
get
(
"query"
):
return
{
"chat_id"
:
chat_id
,
"query"
:
""
,
"children"
:
[],
"children_direct"
:
[],
"children_expanded"
:
[],
"subsections"
:
[],
"sections"
:
[],
"neighbors"
:
[],
}
def
pack
(
items
:
list
[
Retrieved
])
->
list
[
dict
]:
return
[
item
.
to_dict
()
for
item
in
items
]
return
{
"chat_id"
:
chat_id
,
"query"
:
result
[
"query"
],
"children"
:
pack
(
result
[
"children"
]),
"children_direct"
:
pack
(
result
[
"children_direct"
]),
"children_expanded"
:
pack
(
result
[
"children_expanded"
]),
"subsections"
:
pack
(
result
[
"subsections"
]),
"sections"
:
pack
(
result
[
"sections"
]),
"neighbors"
:
pack
(
result
[
"neighbors"
]),
}
math-tutor/backend/app/main.py
View file @
e02dd127
...
...
@@ -2,7 +2,7 @@ from contextlib import asynccontextmanager
from
fastapi
import
FastAPI
from
fastapi.middleware.cors
import
CORSMiddleware
from
app.api
import
canvas
,
chat
,
health
,
retrieval
from
app.api
import
canvas
,
chat
,
health
,
retrieval
,
context
from
app.config
import
get_frontend_url
...
...
@@ -25,4 +25,5 @@ app.add_middleware(
app
.
include_router
(
chat
.
router
)
app
.
include_router
(
canvas
.
router
)
app
.
include_router
(
retrieval
.
router
)
app
.
include_router
(
context
.
router
)
app
.
include_router
(
health
.
router
)
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