Replies: 1 comment
-
@guyouyue Hello there! I'm here to help you with any bugs, questions, or contributions you have. Let's work together to resolve the issue you're facing. 要在你的 agent 中模仿
以下是一个示例,展示了如何定义和使用自定义工具: from langchain_core.tools import BaseTool
from langchain_core.pydantic_v1 import root_validator
from typing import List
class RoleDirectChatTool(BaseTool):
name: str = "RoleDirectChatTool"
description: str = "A tool for direct chat with role-based context."
chat_history_list_chunk: str
model_name: str
message_list: List[str]
session_id: int
task_id: str
@root_validator(pre=True)
def validate_params(cls, values):
if not values.get("chat_history_list_chunk"):
raise ValueError("chat_history_list_chunk is required")
if not values.get("model_name"):
raise ValueError("model_name is required")
if not values.get("message_list"):
raise ValueError("message_list is required")
if not values.get("session_id"):
raise ValueError("session_id is required")
if not values.get("task_id"):
raise ValueError("task_id is required")
return values
def _run(self) -> List[str]:
return cuser_content_direct_chat(
self.chat_history_list_chunk,
self.model_name,
self.message_list,
self.session_id,
self.task_id
)
# 初始化 agent
async def agent_chat(chat_history_list_chunk, model_name, message_list, session_id, task_id):
llm_session, model_id = llm_utils.get_easy_openai_llm_session(model_name)
tools = [
RoleDirectChatTool(
chat_history_list_chunk=chat_history_list_chunk,
model_name=model_name,
message_list=message_list,
session_id=session_id,
task_id=task_id
),
]
agent_executor = initialize_agent(
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
llm=llm_session,
tools=tools,
verbose=True,
callbacks=[SqlStdOutCallbackHandler(session_id, task_id, model_id)],
return_intermediate_steps=True
)
bot_content = agent_executor.run(
message_list,
session_id,
task_id,
chat_history_list_chunk,
model_name,
model_id,
version="v1"
)
return bot_content.get("output") 确保在使用 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Checked other resources
Commit to Help
Example Code
Description
我agent中在模仿SteamshipImageGenerationTool写自带参数的tool,结果异常,但方法功能直接在chain中调用是正常运行的
System Info
python -m langchain_core.sys_info
Beta Was this translation helpful? Give feedback.
All reactions