add README
This commit is contained in:
parent
88e3b613e2
commit
1007f3e9b0
|
|
@ -1,8 +0,0 @@
|
||||||
# 默认忽略的文件
|
|
||||||
/shelf/
|
|
||||||
/workspace.xml
|
|
||||||
# 基于编辑器的 HTTP 客户端请求
|
|
||||||
/httpRequests/
|
|
||||||
# Datasource local storage ignored files
|
|
||||||
/dataSources/
|
|
||||||
/dataSources.local.xml
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="com.codeverse.userSettings.MarscodeWorkspaceAppSettingsState">
|
|
||||||
<option name="chatAppRouterInfo" value="builder/69a1edffb0d7ec083146bf9b" />
|
|
||||||
<option name="progress" value="1.0" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<module type="PYTHON_MODULE" version="4">
|
|
||||||
<component name="NewModuleRootManager">
|
|
||||||
<content url="file://$MODULE_DIR$" />
|
|
||||||
<orderEntry type="jdk" jdkName="edu" jdkType="Python SDK" />
|
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
|
||||||
</component>
|
|
||||||
</module>
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
<component name="InspectionProjectProfileManager">
|
|
||||||
<profile version="1.0">
|
|
||||||
<option name="myName" value="Project Default" />
|
|
||||||
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
|
||||||
<option name="ignoredPackages">
|
|
||||||
<value>
|
|
||||||
<list size="18">
|
|
||||||
<item index="0" class="java.lang.String" itemvalue="pandas" />
|
|
||||||
<item index="1" class="java.lang.String" itemvalue="pyyaml" />
|
|
||||||
<item index="2" class="java.lang.String" itemvalue="scipy" />
|
|
||||||
<item index="3" class="java.lang.String" itemvalue="future" />
|
|
||||||
<item index="4" class="java.lang.String" itemvalue="torchdiffeq" />
|
|
||||||
<item index="5" class="java.lang.String" itemvalue="pytorch" />
|
|
||||||
<item index="6" class="java.lang.String" itemvalue="numpy" />
|
|
||||||
<item index="7" class="java.lang.String" itemvalue="tqdm" />
|
|
||||||
<item index="8" class="java.lang.String" itemvalue="sktime" />
|
|
||||||
<item index="9" class="java.lang.String" itemvalue="patool" />
|
|
||||||
<item index="10" class="java.lang.String" itemvalue="scikit-learn" />
|
|
||||||
<item index="11" class="java.lang.String" itemvalue="PyWavelets" />
|
|
||||||
<item index="12" class="java.lang.String" itemvalue="matplotlib" />
|
|
||||||
<item index="13" class="java.lang.String" itemvalue="reformer-pytorch" />
|
|
||||||
<item index="14" class="java.lang.String" itemvalue="torch" />
|
|
||||||
<item index="15" class="java.lang.String" itemvalue="einops" />
|
|
||||||
<item index="16" class="java.lang.String" itemvalue="local-attention" />
|
|
||||||
<item index="17" class="java.lang.String" itemvalue="sympy" />
|
|
||||||
</list>
|
|
||||||
</value>
|
|
||||||
</option>
|
|
||||||
</inspection_tool>
|
|
||||||
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
|
||||||
<option name="ignoredErrors">
|
|
||||||
<list>
|
|
||||||
<option value="N803" />
|
|
||||||
<option value="N806" />
|
|
||||||
</list>
|
|
||||||
</option>
|
|
||||||
</inspection_tool>
|
|
||||||
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
|
|
||||||
<option name="processCode" value="true" />
|
|
||||||
<option name="processLiterals" value="true" />
|
|
||||||
<option name="processComments" value="true" />
|
|
||||||
</inspection_tool>
|
|
||||||
</profile>
|
|
||||||
</component>
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
<component name="InspectionProjectProfileManager">
|
|
||||||
<settings>
|
|
||||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
|
||||||
<version value="1.0" />
|
|
||||||
</settings>
|
|
||||||
</component>
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="Black">
|
|
||||||
<option name="sdkName" value="Python 3.12 (Eula)" />
|
|
||||||
</component>
|
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="edu" project-jdk-type="Python SDK" />
|
|
||||||
</project>
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectModuleManager">
|
|
||||||
<modules>
|
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/agent_proj.iml" filepath="$PROJECT_DIR$/.idea/agent_proj.iml" />
|
|
||||||
</modules>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
26
01_Agent.py
26
01_Agent.py
|
|
@ -1,26 +0,0 @@
|
||||||
from langchain.agents import create_agent
|
|
||||||
from langchain_community.chat_models.tongyi import ChatTongyi
|
|
||||||
from langchain_core.tools import tool
|
|
||||||
|
|
||||||
@tool(description="查询天气")
|
|
||||||
def get_weather():
|
|
||||||
return "晴天"
|
|
||||||
|
|
||||||
|
|
||||||
agent = create_agent(
|
|
||||||
model=ChatTongyi(model="qwen3-max"),
|
|
||||||
tools=[get_weather],
|
|
||||||
system_prompt="你是一个聊天助手,可以回答用户问题"
|
|
||||||
)
|
|
||||||
|
|
||||||
res = agent.invoke(
|
|
||||||
{
|
|
||||||
"messages": [
|
|
||||||
{"role": "user", "content": "明天深圳的天气如何"},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
for msg in res["messages"]:
|
|
||||||
print(f"{type(msg).__name__}: {msg.content}")
|
|
||||||
|
|
||||||
37
02_Stream.py
37
02_Stream.py
|
|
@ -1,37 +0,0 @@
|
||||||
from langchain.agents import create_agent
|
|
||||||
from langchain_community.chat_models.tongyi import ChatTongyi
|
|
||||||
from langchain_core.tools import tool
|
|
||||||
|
|
||||||
|
|
||||||
@tool(description="查询股票价格")
|
|
||||||
def get_price(name: str) -> str:
|
|
||||||
return f"股票{name}的价格是20元"
|
|
||||||
|
|
||||||
|
|
||||||
@tool(description="查询股票信息")
|
|
||||||
def get_info(name: str) -> str:
|
|
||||||
return f"股票{name}是一家A股上市公司"
|
|
||||||
|
|
||||||
|
|
||||||
agent = create_agent(
|
|
||||||
model=ChatTongyi(model="qwen3-max"),
|
|
||||||
tools=[get_price, get_info],
|
|
||||||
system_prompt="你是一个智能助手,可以回答股票相关问题,请告知我思考过程,让我知道你为什么调用某个工具"
|
|
||||||
)
|
|
||||||
|
|
||||||
res = agent.stream(
|
|
||||||
{"messages": [{
|
|
||||||
"role": "user", "content": "kk公司股价多少,并介绍一下"
|
|
||||||
}]},
|
|
||||||
stream_mode="values"
|
|
||||||
)
|
|
||||||
|
|
||||||
for chunk in res:
|
|
||||||
last_msg = chunk["messages"][-1]
|
|
||||||
if last_msg.content:
|
|
||||||
print(type(last_msg).__name__, last_msg.content)
|
|
||||||
|
|
||||||
if type(last_msg).__name__ == "AIMessage":
|
|
||||||
if last_msg.tool_calls:
|
|
||||||
tool_list = [tc['name'] for tc in last_msg.tool_calls]
|
|
||||||
print(f"工具调佣 {tool_list}")
|
|
||||||
39
03_ReAct.py
39
03_ReAct.py
|
|
@ -1,39 +0,0 @@
|
||||||
from langchain.agents import create_agent
|
|
||||||
from langchain_community.chat_models.tongyi import ChatTongyi
|
|
||||||
from langchain_core.tools import tool
|
|
||||||
|
|
||||||
|
|
||||||
@tool(description="获取体重,返回值整数,单位千克")
|
|
||||||
def get_weight() -> int:
|
|
||||||
return 93
|
|
||||||
|
|
||||||
|
|
||||||
@tool(description="获取身高,返回值整数,单位厘米")
|
|
||||||
def get_height() -> int:
|
|
||||||
return 185
|
|
||||||
|
|
||||||
|
|
||||||
agent = create_agent(
|
|
||||||
model=ChatTongyi(model="qwen3-max"),
|
|
||||||
tools=[get_height, get_weight],
|
|
||||||
system_prompt="""你是严格遵循ReAct框架的智能体,必须按[思考,行动,观察,再思考]的流程解决问题
|
|
||||||
每轮仅能思考并调用1个工具,禁止单词调用多个工具。并告知我你的思考过程,工具调用的原因,按思考、行动
|
|
||||||
、观察三个结构告知我"""
|
|
||||||
)
|
|
||||||
|
|
||||||
res = agent.stream(
|
|
||||||
{"messages": [{
|
|
||||||
"role": "user", "content": "计算我的BMI"
|
|
||||||
}]},
|
|
||||||
stream_mode="values"
|
|
||||||
)
|
|
||||||
|
|
||||||
for chunk in res:
|
|
||||||
last_msg = chunk["messages"][-1]
|
|
||||||
if last_msg.content:
|
|
||||||
print(type(last_msg).__name__, last_msg.content)
|
|
||||||
|
|
||||||
if type(last_msg).__name__ == "AIMessage":
|
|
||||||
if last_msg.tool_calls:
|
|
||||||
tool_list = [tc['name'] for tc in last_msg.tool_calls]
|
|
||||||
print(f"工具调佣 {tool_list}")
|
|
||||||
|
|
@ -1,70 +0,0 @@
|
||||||
"""
|
|
||||||
agent 前后
|
|
||||||
model 前后
|
|
||||||
工具 中
|
|
||||||
模型 中
|
|
||||||
"""
|
|
||||||
|
|
||||||
from langchain.agents import create_agent, AgentState
|
|
||||||
from langchain.agents.middleware import before_agent, after_agent, before_model, after_model, wrap_model_call, \
|
|
||||||
wrap_tool_call
|
|
||||||
from langchain_community.chat_models.tongyi import ChatTongyi
|
|
||||||
from langchain_core.tools import tool
|
|
||||||
from langgraph.runtime import Runtime
|
|
||||||
|
|
||||||
|
|
||||||
@tool(description="查询天气, 传入城市名称字符串,返回字符串天气信息")
|
|
||||||
def get_weather(city: str) -> str:
|
|
||||||
return f"{city} : 晴天"
|
|
||||||
|
|
||||||
|
|
||||||
@before_agent
|
|
||||||
def log_before_agent(state: AgentState, runtime: Runtime) -> None:
|
|
||||||
print(f"before agent: info_num: {len(state["messages"])}")
|
|
||||||
|
|
||||||
|
|
||||||
@after_agent
|
|
||||||
def log_after_agent(state: AgentState, runtime: Runtime) -> None:
|
|
||||||
print(f"after agent: info_num: {len(state["messages"])}")
|
|
||||||
|
|
||||||
|
|
||||||
@before_model
|
|
||||||
def log_before_model(state: AgentState, runtime: Runtime) -> None:
|
|
||||||
print(f"before model: info_num: {len(state["messages"])}")
|
|
||||||
|
|
||||||
|
|
||||||
@after_model
|
|
||||||
def log_after_model(state: AgentState, runtime: Runtime) -> None:
|
|
||||||
print(f"after model: info_num: {len(state["messages"])}")
|
|
||||||
|
|
||||||
|
|
||||||
@wrap_model_call
|
|
||||||
def model_call_hook(request, handler):
|
|
||||||
print(f"model call: {request}")
|
|
||||||
return handler(request)
|
|
||||||
|
|
||||||
|
|
||||||
@wrap_tool_call
|
|
||||||
def model_tool_hook(request, handler):
|
|
||||||
print(f"model tool: {request.tool_call['name']}")
|
|
||||||
print(f"args: {request.tool_call['args']}")
|
|
||||||
return handler(request)
|
|
||||||
|
|
||||||
agent = create_agent(
|
|
||||||
model=ChatTongyi(model="qwen3-max"),
|
|
||||||
tools=[get_weather],
|
|
||||||
middleware=[model_call_hook, model_tool_hook, log_before_model,
|
|
||||||
log_after_model, log_before_agent, log_after_agent],
|
|
||||||
system_prompt="""你是严格遵循ReAct框架的智能体,必须按[思考,行动,观察,再思考]的流程解决问题
|
|
||||||
每轮仅能思考并调用1个工具,禁止单词调用多个工具。并告知我你的思考过程,工具调用的原因,按思考、行动
|
|
||||||
、观察三个结构告知我"""
|
|
||||||
)
|
|
||||||
|
|
||||||
res = agent.stream(
|
|
||||||
{"messages": [{
|
|
||||||
"role": "user", "content": "查询北京的天气"
|
|
||||||
}]},
|
|
||||||
stream_mode="values"
|
|
||||||
)
|
|
||||||
for chunk in res:
|
|
||||||
print(chunk["messages"][-1].content)
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue