添加图片输入
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import os
|
||||
import asyncio
|
||||
|
||||
|
||||
from agentscope.agent import ReActAgent, UserAgent
|
||||
from agentscope.agent import ReActAgent
|
||||
from agentscope.formatter import OpenAIChatFormatter
|
||||
from agentscope.memory import InMemoryMemory
|
||||
from agentscope.model import OpenAIChatModel
|
||||
from agentscope.tool import ToolResponse, Toolkit, execute_python_code
|
||||
from agentscope.tool import Toolkit, execute_python_code
|
||||
from agentscope.message import ImageBlock, Msg, TextBlock, URLSource
|
||||
|
||||
LLAMA_CPP_BASE_URL = os.getenv("LLAMA_CPP_BASE_URL", "http://yyplab.site:8033/v1")
|
||||
@@ -15,8 +14,6 @@ LLAMA_CPP_API_KEY = os.getenv("LLAMA_CPP_API_KEY", "EMPTY")
|
||||
|
||||
|
||||
async def main():
|
||||
"""创建一个 ReAct 智能体并运行一个简单任务。"""
|
||||
# 准备工具
|
||||
toolkit = Toolkit()
|
||||
toolkit.register_tool_function(execute_python_code)
|
||||
|
||||
@@ -26,22 +23,46 @@ async def main():
|
||||
model=OpenAIChatModel(
|
||||
model_name=LLAMA_CPP_MODEL_NAME,
|
||||
api_key=LLAMA_CPP_API_KEY,
|
||||
client_kwargs={"base_url": LLAMA_CPP_BASE_URL}
|
||||
client_kwargs={"base_url": LLAMA_CPP_BASE_URL},
|
||||
stream=True,
|
||||
),
|
||||
memory=InMemoryMemory(),
|
||||
formatter=OpenAIChatFormatter(),
|
||||
toolkit=toolkit,
|
||||
)
|
||||
|
||||
user = UserAgent(name="user")
|
||||
|
||||
msg = await user()
|
||||
while True:
|
||||
msg = await yyp_bot(msg)
|
||||
msg = await user(msg)
|
||||
if msg.get_text_content() == "exit":
|
||||
user_text = input("请输入问题:").strip()
|
||||
if user_text == "exit":
|
||||
break
|
||||
|
||||
image_path = input("请输入图片路径,留空则不发送图片:").strip()
|
||||
|
||||
if image_path:
|
||||
msg = Msg(
|
||||
name="user",
|
||||
role="user",
|
||||
content=[
|
||||
TextBlock(
|
||||
type="text",
|
||||
text=user_text,
|
||||
),
|
||||
ImageBlock(
|
||||
type="image",
|
||||
source=URLSource(
|
||||
type="url",
|
||||
url=image_path,
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
else:
|
||||
msg = Msg(
|
||||
name="user",
|
||||
role="user",
|
||||
content=user_text,
|
||||
)
|
||||
|
||||
await yyp_bot(msg)
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user