-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_completion.py
More file actions
57 lines (49 loc) · 1.69 KB
/
Copy pathagent_completion.py
File metadata and controls
57 lines (49 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from typing import List
from openrouter_settings import RouterModel, RouterConfig, client
from openai import ChatCompletion
import time
import structlog
logger = structlog.get_logger(__name__)
MODEL = RouterModel.MINIMAX25.value
def agent_complete(
messages: List[dict],
tools: List[dict] = None,
model: str = MODEL,
max_tokens: int = 500,
temperature: float = 0.3,
reasoning_effort: str = "medium", # "low", "medium", "high", "xhigh"
usage_instance=None,
) -> ChatCompletion:
str_time = time.time()
router_config = RouterConfig.config(model)
if reasoning_effort:
router_config["reasoning"] = {"effort": reasoning_effort}
if "anthropic" in model.lower():
router_config["reasoning"] = {
"max_tokens": max_tokens // 1.5
}
completion = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
temperature=temperature,
max_tokens=max_tokens,
extra_body=router_config,
)
choice = completion.choices[0]
runtime = time.time() - str_time
sys_msg = messages[0]["content"] if messages else ""
sys_summary = f"...{sys_msg[-1000:]}" if len(sys_msg) > 1000 else sys_msg
logger.info(
"[Agentic] Completion done",
completion_message=choice.message,
message_system_summary=sys_summary,
messages_preview=messages[1:][-5:], # last 5 non-system messages
message_len=len(messages),
max_tokens=max_tokens,
model_completion=completion.model,
model_requested=model,
provider=completion.provider,
runtime=runtime,
)
return completion