20250912
This commit is contained in:
@@ -8,3 +8,5 @@ llama-index
|
||||
llama-index-core
|
||||
llama-index-llms-dashscope
|
||||
llama-index-indices-managed-dashscope
|
||||
|
||||
qwen-agent
|
||||
272
test/langgraph/quickstart_graph_api.ipynb
Normal file
272
test/langgraph/quickstart_graph_api.ipynb
Normal file
File diff suppressed because one or more lines are too long
144
test/langgraph/quickstart_graph_api.py
Normal file
144
test/langgraph/quickstart_graph_api.py
Normal file
@@ -0,0 +1,144 @@
|
||||
# Step 0: Define tools and model
|
||||
|
||||
from langchain_core.tools import tool
|
||||
from langchain_community.chat_models.tongyi import ChatTongyi
|
||||
|
||||
llm = ChatTongyi(
|
||||
model="qwen-max", # 此处以qwen-max为例,您可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
|
||||
streaming=True,
|
||||
# other params...
|
||||
)
|
||||
|
||||
# Define tools
|
||||
@tool
|
||||
def multiply(a: int, b: int) -> int:
|
||||
"""Multiply a and b.
|
||||
|
||||
Args:
|
||||
a: first int
|
||||
b: second int
|
||||
"""
|
||||
return a * b
|
||||
|
||||
|
||||
@tool
|
||||
def add(a: int, b: int) -> int:
|
||||
"""Adds a and b.
|
||||
|
||||
Args:
|
||||
a: first int
|
||||
b: second int
|
||||
"""
|
||||
return a + b
|
||||
|
||||
|
||||
@tool
|
||||
def divide(a: int, b: int) -> float:
|
||||
"""Divide a and b.
|
||||
|
||||
Args:
|
||||
a: first int
|
||||
b: second int
|
||||
"""
|
||||
return a / b
|
||||
|
||||
|
||||
# Augment the LLM with tools
|
||||
tools = [add, multiply, divide]
|
||||
tools_by_name = {tool.name: tool for tool in tools}
|
||||
llm_with_tools = llm.bind_tools(tools)
|
||||
|
||||
# Step 1: Define state
|
||||
|
||||
from langchain_core.messages import AnyMessage
|
||||
from typing_extensions import TypedDict, Annotated
|
||||
import operator
|
||||
|
||||
class MessagesState(TypedDict):
|
||||
messages: Annotated[list[AnyMessage], operator.add]
|
||||
llm_calls: int
|
||||
|
||||
# Step 2: Define model node
|
||||
from langchain_core.messages import SystemMessage
|
||||
def llm_call(state: dict):
|
||||
"""LLM decides whether to call a tool or not"""
|
||||
|
||||
return {
|
||||
"messages": [
|
||||
llm_with_tools.invoke(
|
||||
[
|
||||
SystemMessage(
|
||||
content="You are a helpful assistant tasked with performing arithmetic on a set of inputs."
|
||||
)
|
||||
]
|
||||
+ state["messages"]
|
||||
)
|
||||
],
|
||||
"llm_calls": state.get('llm_calls', 0) + 1
|
||||
}
|
||||
|
||||
|
||||
# Step 3: Define tool node
|
||||
|
||||
|
||||
from langchain_core.messages import ToolMessage
|
||||
|
||||
def tool_node(state: dict):
|
||||
"""Performs the tool call"""
|
||||
|
||||
result = []
|
||||
for tool_call in state["messages"][-1].tool_calls:
|
||||
tool = tools_by_name[tool_call["name"]]
|
||||
observation = tool.invoke(tool_call["args"])
|
||||
result.append(ToolMessage(content=observation, tool_call_id=tool_call["id"]))
|
||||
return {"messages": result}
|
||||
|
||||
# Step 4: Define logic to determine whether to end
|
||||
|
||||
from typing import Literal
|
||||
from langgraph.graph import StateGraph, START, END
|
||||
|
||||
# Conditional edge function to route to the tool node or end based upon whether the LLM made a tool call
|
||||
def should_continue(state: MessagesState) -> Literal["tool_node", END]:
|
||||
"""Decide if we should continue the loop or stop based upon whether the LLM made a tool call"""
|
||||
|
||||
messages = state["messages"]
|
||||
last_message = messages[-1]
|
||||
# If the LLM makes a tool call, then perform an action
|
||||
if last_message.tool_calls:
|
||||
return "tool_node"
|
||||
# Otherwise, we stop (reply to the user)
|
||||
return END
|
||||
|
||||
# Step 5: Build agent
|
||||
|
||||
# Build workflow
|
||||
agent_builder = StateGraph(MessagesState)
|
||||
|
||||
# Add nodes
|
||||
agent_builder.add_node("llm_call", llm_call)
|
||||
agent_builder.add_node("tool_node", tool_node)
|
||||
|
||||
# Add edges to connect nodes
|
||||
agent_builder.add_edge(START, "llm_call")
|
||||
agent_builder.add_conditional_edges(
|
||||
"llm_call",
|
||||
should_continue,
|
||||
["tool_node", END]
|
||||
)
|
||||
agent_builder.add_edge("tool_node", "llm_call")
|
||||
|
||||
# Compile the agent
|
||||
agent = agent_builder.compile()
|
||||
|
||||
|
||||
from IPython.display import Image, display
|
||||
# Show the agent
|
||||
display(Image(agent.get_graph(xray=True).draw_mermaid_png()))
|
||||
|
||||
# Invoke
|
||||
# from langchain_core.messages import HumanMessage
|
||||
# messages = [HumanMessage(content="Add 3 and 4.")]
|
||||
# messages = agent.invoke({"messages": messages})
|
||||
# for m in messages["messages"]:
|
||||
# m.pretty_print()
|
||||
205
test/langgraph/quistart_functional_api.ipynb
Normal file
205
test/langgraph/quistart_functional_api.ipynb
Normal file
@@ -0,0 +1,205 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "initial_id",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-09-12T15:52:56.909385Z",
|
||||
"start_time": "2025-09-12T15:52:56.417088Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"os.environ['DASHSCOPE_API_KEY'] = 'sk-e2a05bbcfac84e53b73f98acef15a009'\n",
|
||||
"\n",
|
||||
"# Step 0: Define tools and model\n",
|
||||
"\n",
|
||||
"from langchain_core.tools import tool\n",
|
||||
"from langchain_community.chat_models.tongyi import ChatTongyi\n",
|
||||
"\n",
|
||||
"llm = ChatTongyi(\n",
|
||||
" model=\"qwen-max\", # 此处以qwen-max为例,您可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models\n",
|
||||
" streaming=True,\n",
|
||||
" # other params...\n",
|
||||
")"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 1
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-09-12T15:54:08.425580Z",
|
||||
"start_time": "2025-09-12T15:54:08.374623Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"\n",
|
||||
"\n",
|
||||
"# Define tools\n",
|
||||
"@tool\n",
|
||||
"def multiply(a: int, b: int) -> int:\n",
|
||||
" \"\"\"Multiply a and b.\n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" a: first int\n",
|
||||
" b: second int\n",
|
||||
" \"\"\"\n",
|
||||
" return a * b\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@tool\n",
|
||||
"def add(a: int, b: int) -> int:\n",
|
||||
" \"\"\"Adds a and b.\n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" a: first int\n",
|
||||
" b: second int\n",
|
||||
" \"\"\"\n",
|
||||
" return a + b\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@tool\n",
|
||||
"def divide(a: int, b: int) -> float:\n",
|
||||
" \"\"\"Divide a and b.\n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" a: first int\n",
|
||||
" b: second int\n",
|
||||
" \"\"\"\n",
|
||||
" return a / b\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Augment the LLM with tools\n",
|
||||
"tools = [add, multiply, divide]\n",
|
||||
"tools_by_name = {tool.name: tool for tool in tools}\n",
|
||||
"llm_with_tools = llm.bind_tools(tools)\n",
|
||||
"\n",
|
||||
"from langgraph.graph import add_messages\n",
|
||||
"from langchain_core.messages import (\n",
|
||||
" SystemMessage,\n",
|
||||
" HumanMessage,\n",
|
||||
" BaseMessage,\n",
|
||||
" ToolCall,\n",
|
||||
")\n",
|
||||
"from langgraph.func import entrypoint, task\n",
|
||||
"\n",
|
||||
"# Step 1: define model node\n",
|
||||
"@task\n",
|
||||
"def call_llm(messages: list[BaseMessage]):\n",
|
||||
" \"\"\"LLM decides whether to call a tool or not\"\"\"\n",
|
||||
" return llm_with_tools.invoke(\n",
|
||||
" [\n",
|
||||
" SystemMessage(\n",
|
||||
" content=\"You are a helpful assistant tasked with performing arithmetic on a set of inputs.\"\n",
|
||||
" )\n",
|
||||
" ]\n",
|
||||
" + messages\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Step 2: define tool node\n",
|
||||
"@task\n",
|
||||
"def call_tool(tool_call: ToolCall):\n",
|
||||
" \"\"\"Performs the tool call\"\"\"\n",
|
||||
" tool = tools_by_name[tool_call[\"name\"]]\n",
|
||||
" return tool.invoke(tool_call)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Step 3: define agent\n",
|
||||
"@entrypoint()\n",
|
||||
"def agent(messages: list[BaseMessage]):\n",
|
||||
" llm_response = call_llm(messages).result()\n",
|
||||
"\n",
|
||||
" while True:\n",
|
||||
" if not llm_response.tool_calls:\n",
|
||||
" break\n",
|
||||
"\n",
|
||||
" # Execute tools\n",
|
||||
" tool_result_futures = [\n",
|
||||
" call_tool(tool_call) for tool_call in llm_response.tool_calls\n",
|
||||
" ]\n",
|
||||
" tool_results = [fut.result() for fut in tool_result_futures]\n",
|
||||
" messages = add_messages(messages, [llm_response, *tool_results])\n",
|
||||
" llm_response = call_llm(messages).result()\n",
|
||||
"\n",
|
||||
" messages = add_messages(messages, llm_response)\n",
|
||||
" return messages"
|
||||
],
|
||||
"id": "8a77a9b24ee9616d",
|
||||
"outputs": [],
|
||||
"execution_count": 2
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-09-12T15:54:11.693756Z",
|
||||
"start_time": "2025-09-12T15:54:10.101700Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"\n",
|
||||
"# Invoke\n",
|
||||
"messages = [HumanMessage(content=\"Add 3 and 4.\")]\n",
|
||||
"for chunk in agent.stream(messages, stream_mode=\"updates\"):\n",
|
||||
" print(chunk)\n",
|
||||
" print(\"\\n\")"
|
||||
],
|
||||
"id": "7c4b06da8200b106",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{'call_llm': AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_ef8c897dd4f84afbbf0927', 'type': 'function', 'function': {'name': 'add', 'arguments': '{\"a\": 3, \"b\": 4}'}}]}, response_metadata={'model_name': 'qwen-max', 'finish_reason': 'tool_calls', 'request_id': '6d8c6555-1a67-4cc9-a93f-57e94bc20842', 'token_usage': {'input_tokens': 354, 'output_tokens': 22, 'total_tokens': 376, 'prompt_tokens_details': {'cached_tokens': 0}}}, id='lc_run--afcea3de-940e-45c6-ba96-bbd7e41fa115-0', tool_calls=[{'name': 'add', 'args': {'a': 3, 'b': 4}, 'id': 'call_ef8c897dd4f84afbbf0927', 'type': 'tool_call'}], chunk_position=None)}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"{'call_tool': ToolMessage(content='7', name='add', id='aeaf3d29-254b-48ab-a933-814e9ea72394', tool_call_id='call_ef8c897dd4f84afbbf0927')}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"{'call_llm': AIMessage(content='The sum of 3 and 4 is 7.', additional_kwargs={}, response_metadata={'model_name': 'qwen-max', 'finish_reason': 'stop', 'request_id': '310102ab-48dc-4e80-bc57-ca8814239a65', 'token_usage': {'input_tokens': 386, 'output_tokens': 13, 'total_tokens': 399, 'prompt_tokens_details': {'cached_tokens': 0}}}, id='lc_run--b3dffae8-42c2-492e-a1f4-e659eba6a879-0', chunk_position=None)}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"{'agent': [HumanMessage(content='Add 3 and 4.', additional_kwargs={}, response_metadata={}, id='40fc3758-a8ab-4302-aff9-8dfbdec16fa0'), AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_ef8c897dd4f84afbbf0927', 'type': 'function', 'function': {'name': 'add', 'arguments': '{\"a\": 3, \"b\": 4}'}}]}, response_metadata={'model_name': 'qwen-max', 'finish_reason': 'tool_calls', 'request_id': '6d8c6555-1a67-4cc9-a93f-57e94bc20842', 'token_usage': {'input_tokens': 354, 'output_tokens': 22, 'total_tokens': 376, 'prompt_tokens_details': {'cached_tokens': 0}}}, id='lc_run--afcea3de-940e-45c6-ba96-bbd7e41fa115-0', tool_calls=[{'name': 'add', 'args': {'a': 3, 'b': 4}, 'id': 'call_ef8c897dd4f84afbbf0927', 'type': 'tool_call'}], chunk_position=None), ToolMessage(content='7', name='add', id='aeaf3d29-254b-48ab-a933-814e9ea72394', tool_call_id='call_ef8c897dd4f84afbbf0927'), AIMessage(content='The sum of 3 and 4 is 7.', additional_kwargs={}, response_metadata={'model_name': 'qwen-max', 'finish_reason': 'stop', 'request_id': '310102ab-48dc-4e80-bc57-ca8814239a65', 'token_usage': {'input_tokens': 386, 'output_tokens': 13, 'total_tokens': 399, 'prompt_tokens_details': {'cached_tokens': 0}}}, id='lc_run--b3dffae8-42c2-492e-a1f4-e659eba6a879-0', chunk_position=None)]}\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 3
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"execution_count": null,
|
||||
"source": "",
|
||||
"id": "7e55492ae0289f06"
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -1,24 +1,36 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "2eeef59eecfab41a",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-27T14:50:29.709398Z",
|
||||
"start_time": "2025-08-27T14:50:28.732112Z"
|
||||
"end_time": "2025-08-27T16:02:52.719750Z",
|
||||
"start_time": "2025-08-27T16:02:51.725308Z"
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from llama_index.llms.dashscope import DashScope\n",
|
||||
"from llama_index.llms.openai import OpenAI\n",
|
||||
"from knightutils.utils.set_proxy import set_proxy_with_os\n",
|
||||
"\n",
|
||||
"set_proxy_with_os()\n",
|
||||
"\n",
|
||||
"os.environ[\"DASHSCOPE_API_KEY\"] = \"sk-e2a05bbcfac84e53b73f98acef15a009\"\n",
|
||||
"# llm = DashScope(model_name=\"qwen-max\", verify=False) # 设置检索引擎生成回答时调用的大模型。\n",
|
||||
"llm = DashScope(model_name=\"qwen-max\") # 设置检索引擎生成回答时调用的大模型。"
|
||||
],
|
||||
"id": "2eeef59eecfab41a",
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"已通过环境变量设置代理\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 1
|
||||
},
|
||||
{
|
||||
@@ -27,8 +39,8 @@
|
||||
"metadata": {
|
||||
"collapsed": true,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-27T14:50:42.901841Z",
|
||||
"start_time": "2025-08-27T14:50:31.035200Z"
|
||||
"end_time": "2025-08-27T16:02:58.073124Z",
|
||||
"start_time": "2025-08-27T16:02:57.170496Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
@@ -37,41 +49,67 @@
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"William Shakespeare (1564-1616) is widely regarded as one of the greatest writers in the English language and one of the world's pre-eminent dramatists. He is often called England's national poet and the \"Bard of Avon.\" His works, including 39 plays, 154 sonnets, and two long narrative poems, have been translated into every major living language and are performed more often than those of any other playwright. Some of his most famous plays include \"Romeo and Juliet,\" \"Hamlet,\" \"Macbeth,\" \"Othello,\" and \"King Lear.\" Shakespeare's influence extends from theater and literature to film, popular culture, and the English language itself, with many phrases and expressions from his works still in use today.\n"
|
||||
"ename": "SSLError",
|
||||
"evalue": "HTTPSConnectionPool(host='dashscope.aliyuncs.com', port=443): Max retries exceeded with url: /api/v1/services/aigc/text-generation/generation (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)')))",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
|
||||
"\u001B[0;31mSSLError\u001B[0m Traceback (most recent call last)",
|
||||
"\u001B[0;31mSSLError\u001B[0m: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)",
|
||||
"\nThe above exception was the direct cause of the following exception:\n",
|
||||
"\u001B[0;31mMaxRetryError\u001B[0m Traceback (most recent call last)",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/requests/adapters.py:644\u001B[0m, in \u001B[0;36mHTTPAdapter.send\u001B[0;34m(self, request, stream, timeout, verify, cert, proxies)\u001B[0m\n\u001B[1;32m 643\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m--> 644\u001B[0m resp \u001B[38;5;241m=\u001B[39m \u001B[43mconn\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43murlopen\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 645\u001B[0m \u001B[43m \u001B[49m\u001B[43mmethod\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mrequest\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mmethod\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 646\u001B[0m \u001B[43m \u001B[49m\u001B[43murl\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43murl\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 647\u001B[0m \u001B[43m \u001B[49m\u001B[43mbody\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mrequest\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mbody\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 648\u001B[0m \u001B[43m \u001B[49m\u001B[43mheaders\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mrequest\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mheaders\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 649\u001B[0m \u001B[43m \u001B[49m\u001B[43mredirect\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43;01mFalse\u001B[39;49;00m\u001B[43m,\u001B[49m\n\u001B[1;32m 650\u001B[0m \u001B[43m \u001B[49m\u001B[43massert_same_host\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43;01mFalse\u001B[39;49;00m\u001B[43m,\u001B[49m\n\u001B[1;32m 651\u001B[0m \u001B[43m \u001B[49m\u001B[43mpreload_content\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43;01mFalse\u001B[39;49;00m\u001B[43m,\u001B[49m\n\u001B[1;32m 652\u001B[0m \u001B[43m \u001B[49m\u001B[43mdecode_content\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43;01mFalse\u001B[39;49;00m\u001B[43m,\u001B[49m\n\u001B[1;32m 653\u001B[0m \u001B[43m \u001B[49m\u001B[43mretries\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mmax_retries\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 654\u001B[0m \u001B[43m \u001B[49m\u001B[43mtimeout\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mtimeout\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 655\u001B[0m \u001B[43m \u001B[49m\u001B[43mchunked\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mchunked\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 656\u001B[0m \u001B[43m \u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 658\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m (ProtocolError, \u001B[38;5;167;01mOSError\u001B[39;00m) \u001B[38;5;28;01mas\u001B[39;00m err:\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/urllib3/connectionpool.py:841\u001B[0m, in \u001B[0;36mHTTPConnectionPool.urlopen\u001B[0;34m(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)\u001B[0m\n\u001B[1;32m 839\u001B[0m new_e \u001B[38;5;241m=\u001B[39m ProtocolError(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mConnection aborted.\u001B[39m\u001B[38;5;124m\"\u001B[39m, new_e)\n\u001B[0;32m--> 841\u001B[0m retries \u001B[38;5;241m=\u001B[39m \u001B[43mretries\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mincrement\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 842\u001B[0m \u001B[43m \u001B[49m\u001B[43mmethod\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43murl\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43merror\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mnew_e\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43m_pool\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43m_stacktrace\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43msys\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mexc_info\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m2\u001B[39;49m\u001B[43m]\u001B[49m\n\u001B[1;32m 843\u001B[0m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 844\u001B[0m retries\u001B[38;5;241m.\u001B[39msleep()\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/urllib3/util/retry.py:519\u001B[0m, in \u001B[0;36mRetry.increment\u001B[0;34m(self, method, url, response, error, _pool, _stacktrace)\u001B[0m\n\u001B[1;32m 518\u001B[0m reason \u001B[38;5;241m=\u001B[39m error \u001B[38;5;129;01mor\u001B[39;00m ResponseError(cause)\n\u001B[0;32m--> 519\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m MaxRetryError(_pool, url, reason) \u001B[38;5;28;01mfrom\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21;01mreason\u001B[39;00m \u001B[38;5;66;03m# type: ignore[arg-type]\u001B[39;00m\n\u001B[1;32m 521\u001B[0m log\u001B[38;5;241m.\u001B[39mdebug(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mIncremented Retry for (url=\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;132;01m%s\u001B[39;00m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m): \u001B[39m\u001B[38;5;132;01m%r\u001B[39;00m\u001B[38;5;124m\"\u001B[39m, url, new_retry)\n",
|
||||
"\u001B[0;31mMaxRetryError\u001B[0m: HTTPSConnectionPool(host='dashscope.aliyuncs.com', port=443): Max retries exceeded with url: /api/v1/services/aigc/text-generation/generation (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)')))",
|
||||
"\nDuring handling of the above exception, another exception occurred:\n",
|
||||
"\u001B[0;31mSSLError\u001B[0m Traceback (most recent call last)",
|
||||
"Cell \u001B[0;32mIn[2], line 1\u001B[0m\n\u001B[0;32m----> 1\u001B[0m response \u001B[38;5;241m=\u001B[39m \u001B[43mllm\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mcomplete\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mWilliam Shakespeare is \u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[1;32m 2\u001B[0m \u001B[38;5;28mprint\u001B[39m(response)\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/llama_index/core/instrumentation/dispatcher.py:260\u001B[0m, in \u001B[0;36mDispatcher.span.<locals>.wrapper\u001B[0;34m(func, instance, args, kwargs)\u001B[0m\n\u001B[1;32m 252\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mspan_enter(\n\u001B[1;32m 253\u001B[0m id_\u001B[38;5;241m=\u001B[39mid_,\n\u001B[1;32m 254\u001B[0m bound_args\u001B[38;5;241m=\u001B[39mbound_args,\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 257\u001B[0m tags\u001B[38;5;241m=\u001B[39mtags,\n\u001B[1;32m 258\u001B[0m )\n\u001B[1;32m 259\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m--> 260\u001B[0m result \u001B[38;5;241m=\u001B[39m \u001B[43mfunc\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 261\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mBaseException\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[1;32m 262\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mevent(SpanDropEvent(span_id\u001B[38;5;241m=\u001B[39mid_, err_str\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mstr\u001B[39m(e)))\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/llama_index/core/llms/callbacks.py:429\u001B[0m, in \u001B[0;36mllm_completion_callback.<locals>.wrap.<locals>.wrapped_llm_predict\u001B[0;34m(_self, *args, **kwargs)\u001B[0m\n\u001B[1;32m 420\u001B[0m event_id \u001B[38;5;241m=\u001B[39m callback_manager\u001B[38;5;241m.\u001B[39mon_event_start(\n\u001B[1;32m 421\u001B[0m CBEventType\u001B[38;5;241m.\u001B[39mLLM,\n\u001B[1;32m 422\u001B[0m payload\u001B[38;5;241m=\u001B[39m{\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 426\u001B[0m },\n\u001B[1;32m 427\u001B[0m )\n\u001B[1;32m 428\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m--> 429\u001B[0m f_return_val \u001B[38;5;241m=\u001B[39m \u001B[43mf\u001B[49m\u001B[43m(\u001B[49m\u001B[43m_self\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43margs\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 430\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mBaseException\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[1;32m 431\u001B[0m callback_manager\u001B[38;5;241m.\u001B[39mon_event_end(\n\u001B[1;32m 432\u001B[0m CBEventType\u001B[38;5;241m.\u001B[39mLLM,\n\u001B[1;32m 433\u001B[0m payload\u001B[38;5;241m=\u001B[39m{EventPayload\u001B[38;5;241m.\u001B[39mEXCEPTION: e},\n\u001B[1;32m 434\u001B[0m event_id\u001B[38;5;241m=\u001B[39mevent_id,\n\u001B[1;32m 435\u001B[0m )\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/llama_index/llms/dashscope/base.py:228\u001B[0m, in \u001B[0;36mDashScope.complete\u001B[0;34m(self, prompt, **kwargs)\u001B[0m\n\u001B[1;32m 226\u001B[0m parameters\u001B[38;5;241m.\u001B[39mpop(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mstream\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m)\n\u001B[1;32m 227\u001B[0m messages \u001B[38;5;241m=\u001B[39m chat_message_to_dashscope_messages([message])\n\u001B[0;32m--> 228\u001B[0m response \u001B[38;5;241m=\u001B[39m \u001B[43mcall_with_messages\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 229\u001B[0m \u001B[43m \u001B[49m\u001B[43mmodel\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mmodel_name\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 230\u001B[0m \u001B[43m \u001B[49m\u001B[43mmessages\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmessages\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 231\u001B[0m \u001B[43m \u001B[49m\u001B[43mapi_key\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mapi_key\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 232\u001B[0m \u001B[43m \u001B[49m\u001B[43mparameters\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mparameters\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 233\u001B[0m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 234\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m dashscope_response_to_completion_response(response)\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/llama_index/llms/dashscope/base.py:83\u001B[0m, in \u001B[0;36mcall_with_messages\u001B[0;34m(model, messages, parameters, api_key, **kwargs)\u001B[0m\n\u001B[1;32m 78\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mImportError\u001B[39;00m:\n\u001B[1;32m 79\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\n\u001B[1;32m 80\u001B[0m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mDashScope is not installed. Please install it with \u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m 81\u001B[0m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m`pip install dashscope`.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m 82\u001B[0m )\n\u001B[0;32m---> 83\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43mGeneration\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mcall\u001B[49m\u001B[43m(\u001B[49m\n\u001B[1;32m 84\u001B[0m \u001B[43m \u001B[49m\u001B[43mmodel\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmodel\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmessages\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmessages\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mapi_key\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mapi_key\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mparameters\u001B[49m\n\u001B[1;32m 85\u001B[0m \u001B[43m\u001B[49m\u001B[43m)\u001B[49m\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/dashscope/aigc/generation.py:138\u001B[0m, in \u001B[0;36mGeneration.call\u001B[0;34m(cls, model, prompt, history, api_key, messages, plugins, workspace, **kwargs)\u001B[0m\n\u001B[1;32m 135\u001B[0m kwargs[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mheaders\u001B[39m\u001B[38;5;124m'\u001B[39m] \u001B[38;5;241m=\u001B[39m headers\n\u001B[1;32m 136\u001B[0m \u001B[38;5;28minput\u001B[39m, parameters \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mcls\u001B[39m\u001B[38;5;241m.\u001B[39m_build_input_parameters(\n\u001B[1;32m 137\u001B[0m model, prompt, history, messages, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[0;32m--> 138\u001B[0m response \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43msuper\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mcall\u001B[49m\u001B[43m(\u001B[49m\u001B[43mmodel\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmodel\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 139\u001B[0m \u001B[43m \u001B[49m\u001B[43mtask_group\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mtask_group\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 140\u001B[0m \u001B[43m \u001B[49m\u001B[43mtask\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mGeneration\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mtask\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 141\u001B[0m \u001B[43m \u001B[49m\u001B[43mfunction\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mfunction\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 142\u001B[0m \u001B[43m \u001B[49m\u001B[43mapi_key\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mapi_key\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 143\u001B[0m \u001B[43m \u001B[49m\u001B[38;5;28;43minput\u001B[39;49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43minput\u001B[39;49m\u001B[43m,\u001B[49m\n\u001B[1;32m 144\u001B[0m \u001B[43m \u001B[49m\u001B[43mworkspace\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mworkspace\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 145\u001B[0m \u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mparameters\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 146\u001B[0m is_stream \u001B[38;5;241m=\u001B[39m kwargs\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mstream\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;28;01mFalse\u001B[39;00m)\n\u001B[1;32m 147\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m is_stream:\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/dashscope/client/base_api.py:146\u001B[0m, in \u001B[0;36mBaseApi.call\u001B[0;34m(cls, model, input, task_group, task, function, api_key, workspace, **kwargs)\u001B[0m\n\u001B[1;32m 138\u001B[0m request \u001B[38;5;241m=\u001B[39m _build_api_request(model\u001B[38;5;241m=\u001B[39mmodel,\n\u001B[1;32m 139\u001B[0m \u001B[38;5;28minput\u001B[39m\u001B[38;5;241m=\u001B[39m\u001B[38;5;28minput\u001B[39m,\n\u001B[1;32m 140\u001B[0m task_group\u001B[38;5;241m=\u001B[39mtask_group,\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 143\u001B[0m api_key\u001B[38;5;241m=\u001B[39mapi_key,\n\u001B[1;32m 144\u001B[0m \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[1;32m 145\u001B[0m \u001B[38;5;66;03m# call request service.\u001B[39;00m\n\u001B[0;32m--> 146\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43mrequest\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mcall\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/dashscope/api_entities/http_request.py:84\u001B[0m, in \u001B[0;36mHttpRequest.call\u001B[0;34m(self)\u001B[0m\n\u001B[1;32m 82\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m (item \u001B[38;5;28;01mfor\u001B[39;00m item \u001B[38;5;129;01min\u001B[39;00m response)\n\u001B[1;32m 83\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m---> 84\u001B[0m output \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mnext\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43mresponse\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 85\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[1;32m 86\u001B[0m \u001B[38;5;28mnext\u001B[39m(response)\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/dashscope/api_entities/http_request.py:303\u001B[0m, in \u001B[0;36mHttpRequest._handle_request\u001B[0;34m(self)\u001B[0m\n\u001B[1;32m 301\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mBaseException\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[1;32m 302\u001B[0m logger\u001B[38;5;241m.\u001B[39merror(e)\n\u001B[0;32m--> 303\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m e\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/dashscope/api_entities/http_request.py:286\u001B[0m, in \u001B[0;36mHttpRequest._handle_request\u001B[0;34m(self)\u001B[0m\n\u001B[1;32m 284\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m 285\u001B[0m logger\u001B[38;5;241m.\u001B[39mdebug(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mRequest body: \u001B[39m\u001B[38;5;132;01m%s\u001B[39;00m\u001B[38;5;124m'\u001B[39m \u001B[38;5;241m%\u001B[39m obj)\n\u001B[0;32m--> 286\u001B[0m response \u001B[38;5;241m=\u001B[39m \u001B[43msession\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mpost\u001B[49m\u001B[43m(\u001B[49m\u001B[43murl\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43murl\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 287\u001B[0m \u001B[43m \u001B[49m\u001B[43mstream\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mstream\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 288\u001B[0m \u001B[43m \u001B[49m\u001B[43mjson\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mobj\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 289\u001B[0m \u001B[43m \u001B[49m\u001B[43mheaders\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43m{\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mheaders\u001B[49m\u001B[43m}\u001B[49m\u001B[43m,\u001B[49m\n\u001B[1;32m 290\u001B[0m \u001B[43m \u001B[49m\u001B[43mtimeout\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mtimeout\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 291\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mmethod \u001B[38;5;241m==\u001B[39m HTTPMethod\u001B[38;5;241m.\u001B[39mGET:\n\u001B[1;32m 292\u001B[0m response \u001B[38;5;241m=\u001B[39m session\u001B[38;5;241m.\u001B[39mget(url\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39murl,\n\u001B[1;32m 293\u001B[0m params\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mdata\u001B[38;5;241m.\u001B[39mparameters,\n\u001B[1;32m 294\u001B[0m headers\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mheaders,\n\u001B[1;32m 295\u001B[0m timeout\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mtimeout)\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/requests/sessions.py:637\u001B[0m, in \u001B[0;36mSession.post\u001B[0;34m(self, url, data, json, **kwargs)\u001B[0m\n\u001B[1;32m 626\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m\u001B[38;5;250m \u001B[39m\u001B[38;5;21mpost\u001B[39m(\u001B[38;5;28mself\u001B[39m, url, data\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m, json\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mNone\u001B[39;00m, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs):\n\u001B[1;32m 627\u001B[0m \u001B[38;5;250m \u001B[39m\u001B[38;5;124mr\u001B[39m\u001B[38;5;124;03m\"\"\"Sends a POST request. Returns :class:`Response` object.\u001B[39;00m\n\u001B[1;32m 628\u001B[0m \n\u001B[1;32m 629\u001B[0m \u001B[38;5;124;03m :param url: URL for the new :class:`Request` object.\u001B[39;00m\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 634\u001B[0m \u001B[38;5;124;03m :rtype: requests.Response\u001B[39;00m\n\u001B[1;32m 635\u001B[0m \u001B[38;5;124;03m \"\"\"\u001B[39;00m\n\u001B[0;32m--> 637\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mrequest\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mPOST\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43murl\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mdata\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mdata\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mjson\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mjson\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/requests/sessions.py:589\u001B[0m, in \u001B[0;36mSession.request\u001B[0;34m(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)\u001B[0m\n\u001B[1;32m 584\u001B[0m send_kwargs \u001B[38;5;241m=\u001B[39m {\n\u001B[1;32m 585\u001B[0m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mtimeout\u001B[39m\u001B[38;5;124m\"\u001B[39m: timeout,\n\u001B[1;32m 586\u001B[0m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mallow_redirects\u001B[39m\u001B[38;5;124m\"\u001B[39m: allow_redirects,\n\u001B[1;32m 587\u001B[0m }\n\u001B[1;32m 588\u001B[0m send_kwargs\u001B[38;5;241m.\u001B[39mupdate(settings)\n\u001B[0;32m--> 589\u001B[0m resp \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43msend\u001B[49m\u001B[43m(\u001B[49m\u001B[43mprep\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43msend_kwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 591\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m resp\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/requests/sessions.py:703\u001B[0m, in \u001B[0;36mSession.send\u001B[0;34m(self, request, **kwargs)\u001B[0m\n\u001B[1;32m 700\u001B[0m start \u001B[38;5;241m=\u001B[39m preferred_clock()\n\u001B[1;32m 702\u001B[0m \u001B[38;5;66;03m# Send the request\u001B[39;00m\n\u001B[0;32m--> 703\u001B[0m r \u001B[38;5;241m=\u001B[39m \u001B[43madapter\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43msend\u001B[49m\u001B[43m(\u001B[49m\u001B[43mrequest\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mkwargs\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m 705\u001B[0m \u001B[38;5;66;03m# Total elapsed time of the request (approximately)\u001B[39;00m\n\u001B[1;32m 706\u001B[0m elapsed \u001B[38;5;241m=\u001B[39m preferred_clock() \u001B[38;5;241m-\u001B[39m start\n",
|
||||
"File \u001B[0;32m~/src/knightutils/.venv/lib/python3.10/site-packages/requests/adapters.py:675\u001B[0m, in \u001B[0;36mHTTPAdapter.send\u001B[0;34m(self, request, stream, timeout, verify, cert, proxies)\u001B[0m\n\u001B[1;32m 671\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m ProxyError(e, request\u001B[38;5;241m=\u001B[39mrequest)\n\u001B[1;32m 673\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(e\u001B[38;5;241m.\u001B[39mreason, _SSLError):\n\u001B[1;32m 674\u001B[0m \u001B[38;5;66;03m# This branch is for urllib3 v1.22 and later.\u001B[39;00m\n\u001B[0;32m--> 675\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m SSLError(e, request\u001B[38;5;241m=\u001B[39mrequest)\n\u001B[1;32m 677\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mConnectionError\u001B[39;00m(e, request\u001B[38;5;241m=\u001B[39mrequest)\n\u001B[1;32m 679\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m ClosedPoolError \u001B[38;5;28;01mas\u001B[39;00m e:\n",
|
||||
"\u001B[0;31mSSLError\u001B[0m: HTTPSConnectionPool(host='dashscope.aliyuncs.com', port=443): Max retries exceeded with url: /api/v1/services/aigc/text-generation/generation (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)')))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 2
|
||||
},
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"execution_count": null,
|
||||
"source": "",
|
||||
"id": "5e8170348d3bad72"
|
||||
"id": "5e8170348d3bad72",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"display_name": ".venv",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -1,13 +1,31 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "raw",
|
||||
"source": "print(\"hello\")",
|
||||
"id": "a5d3b9e1d4e6588f"
|
||||
"cell_type": "code",
|
||||
"id": "48072a19c44a9164",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-08-27T16:02:46.279589Z",
|
||||
"start_time": "2025-08-27T16:02:46.277036Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"print(\"hello\")"
|
||||
],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"hello\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 2
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "initial_id",
|
||||
"metadata": {
|
||||
"collapsed": true,
|
||||
@@ -15,28 +33,27 @@
|
||||
"is_executing": true
|
||||
}
|
||||
},
|
||||
"source": "",
|
||||
"outputs": [],
|
||||
"execution_count": null
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"display_name": ".venv",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.6"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
77
test/qwenagent/start.py
Normal file
77
test/qwenagent/start.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import pprint
|
||||
import urllib.parse
|
||||
import json5
|
||||
from qwen_agent.agents import Assistant
|
||||
from qwen_agent.tools.base import BaseTool, register_tool
|
||||
from qwen_agent.utils.output_beautify import typewriter_print
|
||||
|
||||
|
||||
# Step 1 (Optional): Add a custom tool named `my_image_gen`.
|
||||
@register_tool('my_image_gen')
|
||||
class MyImageGen(BaseTool):
|
||||
# The `description` tells the agent the functionality of this tool.
|
||||
description = 'AI painting (image generation) service, input text description, and return the image URL drawn based on text information.'
|
||||
# The `parameters` tell the agent what input parameters the tool has.
|
||||
parameters = [{
|
||||
'name': 'prompt',
|
||||
'type': 'string',
|
||||
'description': 'Detailed description of the desired image content, in English',
|
||||
'required': True
|
||||
}]
|
||||
|
||||
def call(self, params: str, **kwargs) -> str:
|
||||
# `params` are the arguments generated by the LLM agent.
|
||||
prompt = json5.loads(params)['prompt']
|
||||
prompt = urllib.parse.quote(prompt)
|
||||
return json5.dumps(
|
||||
{'image_url': f'https://image.pollinations.ai/prompt/{prompt}'},
|
||||
ensure_ascii=False)
|
||||
|
||||
|
||||
# Step 2: Configure the LLM you are using.
|
||||
llm_cfg = {
|
||||
# Use the model service provided by DashScope:
|
||||
'model': 'qwen-max-latest',
|
||||
'model_type': 'qwen_dashscope',
|
||||
# 'api_key': 'YOUR_DASHSCOPE_API_KEY',
|
||||
# It will use the `DASHSCOPE_API_KEY' environment variable if 'api_key' is not set here.
|
||||
|
||||
# Use a model service compatible with the OpenAI API, such as vLLM or Ollama:
|
||||
# 'model': 'Qwen2.5-7B-Instruct',
|
||||
# 'model_server': 'http://localhost:8000/v1', # base_url, also known as api_base
|
||||
# 'api_key': 'EMPTY',
|
||||
|
||||
# (Optional) LLM hyperparameters for generation:
|
||||
'generate_cfg': {
|
||||
'top_p': 0.8
|
||||
}
|
||||
}
|
||||
|
||||
# Step 3: Create an agent. Here we use the `Assistant` agent as an example, which is capable of using tools and reading files.
|
||||
system_instruction = '''After receiving the user's request, you should:
|
||||
- first draw an image and obtain the image url,
|
||||
- then run code `request.get(image_url)` to download the image,
|
||||
- and finally select an image operation from the given document to process the image.
|
||||
Please show the image using `plt.show()`.'''
|
||||
tools = ['my_image_gen', 'code_interpreter'] # `code_interpreter` is a built-in tool for executing code.
|
||||
files = ['./examples/resource/doc.pdf'] # Give the bot a PDF file to read.
|
||||
bot = Assistant(llm=llm_cfg,
|
||||
system_message=system_instruction,
|
||||
function_list=tools,
|
||||
files=files)
|
||||
|
||||
# Step 4: Run the agent as a chatbot.
|
||||
messages = [] # This stores the chat history.
|
||||
while True:
|
||||
# For example, enter the query "draw a dog and rotate it 90 degrees".
|
||||
query = input('\nuser query: ')
|
||||
# Append the user query to the chat history.
|
||||
messages.append({'role': 'user', 'content': query})
|
||||
response = []
|
||||
response_plain_text = ''
|
||||
print('bot response:')
|
||||
for response in bot.run(messages=messages):
|
||||
# Streaming output.
|
||||
response_plain_text = typewriter_print(response, response_plain_text)
|
||||
# Append the bot responses to the chat history.
|
||||
messages.extend(response)
|
||||
Reference in New Issue
Block a user