forked from trpc-group/trpc-agent-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
64 lines (47 loc) · 1.93 KB
/
Copy pathagent.py
File metadata and controls
64 lines (47 loc) · 1.93 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
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
import os
from trpc_agent_sdk.agents import LlmAgent
from trpc_agent_sdk.models import LLMModel
from trpc_agent_sdk.models import OpenAIModel
from .prompts import INSTRUCTION
from .tools import HobbyToolSet
# =============================================================================
# 2. Create Agent
# =============================================================================
def _get_model_config() -> tuple[str, str, str]:
"""Get model config from environment variables"""
api_key = os.getenv('TRPC_AGENT_API_KEY', '')
url = os.getenv('TRPC_AGENT_BASE_URL', '')
model_name = os.getenv('TRPC_AGENT_MODEL_NAME', '')
if not api_key or not url or not model_name:
raise ValueError('''TRPC_AGENT_API_KEY, TRPC_AGENT_BASE_URL,
and TRPC_AGENT_MODEL_NAME must be set in environment variables''')
return api_key, url, model_name
def _create_model() -> LLMModel:
""" Create a model"""
api_key, url, model_name = _get_model_config()
model = OpenAIModel(model_name=model_name, api_key=api_key, base_url=url)
return model
def create_agent():
"""Create an agent related to hobbies"""
model = _create_model()
# Get the registered toolkit
hobby_toolset = HobbyToolSet()
# Initialize the Toolkit
if hobby_toolset:
hobby_toolset.initialize()
return LlmAgent(
name="hobby_toolset_agent",
description="""A virtual person who loves life, select the appropriate tool based on the user's
interest to obtain interest information, and provide friendly replies.""",
model=model,
tools=[hobby_toolset],
parallel_tool_calls=True,
instruction=INSTRUCTION,
)
root_agent = create_agent()