Skip to content

Commit a817cfb

Browse files
committed
arch(core): use fastapi for launching consumers
1 parent be4a599 commit a817cfb

7 files changed

Lines changed: 66 additions & 14 deletions

File tree

src/server/core/acontext_core/llm/agent/task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async def build_task_ctx(
7272
current_tasks, eil = r.unpack()
7373
if eil:
7474
return r
75-
LOG.info(
75+
LOG.debug(
7676
f"Built task context {[(t.order, t.status.value, t.task_description) for t in current_tasks]}"
7777
)
7878
use_ctx = TaskCtx(

src/server/core/acontext_core/llm/complete/openai_sdk.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ async def openai_complete(
5353
timeout=DEFAULT_CORE_CONFIG.llm_response_timeout,
5454
max_tokens=max_tokens,
5555
tools=tools,
56+
**DEFAULT_CORE_CONFIG.llm_openai_completion_kwargs,
5657
**kwargs,
5758
)
5859
cached_tokens = getattr(response.usage.prompt_tokens_details, "cached_tokens", None)

src/server/core/acontext_core/llm/prompt/task.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,16 @@ def system_prompt(cls) -> str:
6565
- Message with ID format: <message id=N> ... </message>, inside the tag is the message content, the id field indicates the message id.
6666
6767
## Report your Thinking
68-
Use extremely brief wordings to report:
69-
1. Any user requirement or planning?
68+
Use extremely brief wordings to report before calling tools:
69+
1. Any planning from agent? Any requirement or task modification from user?
7070
2. Does the user report that any task failed and need to re-run?
7171
3. How existing tasks are related to current conversation?
72-
4. Any new task is created?
72+
4. Any new task should be created?
7373
5. Which Messages are contributed to planning?
7474
6. Which of them are contributed to which task?
7575
7. Which task's status/description need to be updated?
7676
8. Briefly describe your tool-call actions to correctly manage the tasks.
77-
78-
Make sure your will call `finish` tool after every tools are called
77+
9. Make sure your will call `finish` tool after every tools are called
7978
"""
8079

8180
@classmethod

src/server/core/acontext_core/llm/prompt/task_sop.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,41 @@ def system_prompt(cls) -> str:
77
return """You're a Tool-calling SOP Agent that read the raw working history between user and agent, and generate a tool-calling SOP for the task.
88
99
## What is a Tool-calling SOP?
10+
### Standard Format (JSON)
11+
{
12+
"use_when": str,
13+
"notes": str,
14+
"sop":[
15+
{"tool_name": str, "argument_template": dict},
16+
...
17+
]
18+
}
19+
### Format Breaking down
20+
- 'use_when': The scenario when this sop maybe used, e.g. 'Broswering xxx.com for items' infos', 'Query Lung disease from Database'
21+
- 'notes': An brief guideline to instruct how to proceed this SOP, maybe containing user requirements, tool-use annotations.
22+
- 'sop': a structured array that contains tool-calling steps in correct order, for each step:
23+
- 'tool_name': exact corresponding tool name from history
24+
- 'argument_template': a json of the certain tool-calling arguments and their values that necessage to this SOP.
25+
For example, "arugment_template": {"website": "xxx.com", "action": "click shopping tab in navbar"}
26+
27+
## Input
28+
### Raw History Input
1029
...
1130
12-
## Input Format
13-
14-
## Output Format
15-
16-
## Thinking Guidelines
31+
### User Planning History
32+
...
1733
34+
## Think before Answer
35+
...
1836
"""
1937

2038
@classmethod
21-
def pack_task_input(cls, *args, **kwargs) -> str:
22-
pass
39+
def pack_task_input(cls, history_messages: str, history_planning: str) -> str:
40+
return f"""## Raw History Input
41+
{history_messages}
42+
## User Planning History
43+
{history_planning}
44+
"""
2345

2446
@classmethod
2547
def prompt_kwargs(cls) -> str:

src/server/core/acontext_core/schema/block/sop_block.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class SOPStep(BaseModel):
77
tool_name: str
8-
tool_arguments_with_placeholder: dict[str, Any]
8+
argument_template: dict[str, Any]
99
props: Optional[dict] = None
1010

1111

src/server/core/acontext_core/schema/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CoreConfig(BaseModel):
1818
llm_base_url: Optional[str] = None
1919
llm_openai_default_query: Optional[Mapping[str, Any]] = None
2020
llm_openai_default_header: Optional[Mapping[str, Any]] = None
21+
llm_openai_completion_kwargs: Mapping[str, Any] = {}
2122
llm_response_timeout: float = 60
2223
llm_sdk: Literal["openai", "anthropic"] = "openai"
2324

src/server/core/api.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import asyncio
2+
from contextlib import asynccontextmanager
3+
from fastapi import FastAPI
4+
from acontext_core.di import setup, cleanup, MQ_CLIENT, LOG, DB_CLIENT, S3_CLIENT
5+
6+
7+
@asynccontextmanager
8+
async def lifespan(app: FastAPI):
9+
# Startup
10+
await setup()
11+
# Run consumer in the background
12+
asyncio.create_task(MQ_CLIENT.start())
13+
yield
14+
# Shutdown
15+
await cleanup()
16+
17+
18+
app = FastAPI(lifespan=lifespan)
19+
20+
21+
@app.get("/health")
22+
async def health() -> str:
23+
if not await MQ_CLIENT.health_check():
24+
return "MQ consumer error"
25+
if not await DB_CLIENT.health_check():
26+
return "DB client error"
27+
if not await S3_CLIENT.health_check():
28+
return "S3 client error"
29+
return "ok"

0 commit comments

Comments
 (0)