GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing doc.agentscope.io — Model - AgentScope Note Go to the end to download the full example code. Model¶In this tutorial, we introduce the model APIs integrated in AgentScope, how to use them and how to integrate new model APIs. The supported model APIs and providers include:
Note When using vLLM, you need to configure the appropriate tool calling parameters for different models during deployment, such as Note For OpenAI-compatible models (e.g. vLLM, Deepseek), developers can use the OpenAIChatModel(client_kwargs={"base_url": "http://localhost:8000/v1"}) Note Model behavior parameters (such as temperature, maximum length, etc.) can be preset in the constructor function via the OpenAIChatModel(generate_kwargs={"temperature": 0.3, "max_tokens": 1000}) To provide unified model interfaces, the above model classes has the following common methods:
Note Different model APIs differ in the input message format, refer to Prompt Formatter for more details. The import asyncio import json import os from agentscope.message import TextBlock, ToolUseBlock, ThinkingBlock, Msg from agentscope.model import ChatResponse, DashScopeChatModel response = ChatResponse( content=[ ThinkingBlock( type="thinking", thinking="I should search for AgentScope on Google.", ), TextBlock(type="text", text="I'll search for AgentScope on Google."), ToolUseBlock( type="tool_use", id="642n298gjna", name="google_search", input={"query": "AgentScope?"}, ), ], ) print(response) ChatResponse(content=[{'type': 'thinking', 'thinking': 'I should search for AgentScope on Google.'}, {'type': 'text', 'text': "I'll search for AgentScope on Google."}, {'type': 'tool_use', 'id': '642n298gjna', 'name': 'google_search', 'input': {'query': 'AgentScope?'}}], id='2026-05-25 08:41:44.019_024cae', created_at='2026-05-25 08:41:44.019', type='chat', usage=None, metadata=None)
Taking async def example_model_call() -> None: """An example of using the DashScopeChatModel.""" model = DashScopeChatModel( model_name="qwen-max", api_key=os.environ["DASHSCOPE_API_KEY"], stream=False, ) res = await model( messages=[ {"role": "user", "content": "Hi!"}, ], ) # You can directly create a ``Msg`` object with the response content msg_res = Msg("Friday", res.content, "assistant") print("The response:", res) print("The response as Msg:", msg_res) asyncio.run(example_model_call()) The response: ChatResponse(content=[{'type': 'text', 'text': 'Hello! How can I assist you today?'}], id='f42a49fa-dacf-98be-83fb-5c1d6be69c18', created_at='2026-05-25 08:41:45.449', type='chat', usage=ChatUsage(input_tokens=10, output_tokens=9, time=1.429052, type='chat', metadata=GenerationUsage(input_tokens=10, output_tokens=9)), metadata=None)
The response as Msg: Msg(id='GvmSwwf9wGz8K3ReR3fuvz', name='Friday', content=[{'type': 'text', 'text': 'Hello! How can I assist you today?'}], role='assistant', metadata={}, timestamp='2026-05-25 08:41:45.449', invocation_id='None')
Streaming¶To enable streaming model, set the Note The streaming mode in AgentScope is designed to be cumulative, meaning the content in each chunk contains all the previous content plus the newly generated content. async def example_streaming() -> None: """An example of using the streaming model.""" model = DashScopeChatModel( model_name="qwen-max", api_key=os.environ["DASHSCOPE_API_KEY"], stream=True, ) generator = await model( messages=[ { "role": "user", "content": "Count from 1 to 20, and just report the number without any other information.", }, ], ) print("The type of the response:", type(generator)) i = 0 async for chunk in generator: print(f"Chunk {i}") print(f"\ttype: {type(chunk.content)}") print(f"\t{chunk}\n") i += 1 asyncio.run(example_streaming()) The type of the response: <class 'async_generator'>
Chunk 0
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:46.446', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=1, time=0.995459, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=1)), metadata=None)
Chunk 1
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1\n2\n'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:46.515', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=4, time=1.064213, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=4)), metadata=None)
Chunk 2
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1\n2\n3\n4'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:46.586', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=7, time=1.13498, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=7)), metadata=None)
Chunk 3
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1\n2\n3\n4\n5\n'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:47.436', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=10, time=1.985567, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=10)), metadata=None)
Chunk 4
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1\n2\n3\n4\n5\n6\n7\n8\n'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:47.721', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=16, time=2.270519, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=16)), metadata=None)
Chunk 5
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n1'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:47.845', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=22, time=2.394715, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=22)), metadata=None)
Chunk 6
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n1'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:48.313', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=28, time=2.862106, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=28)), metadata=None)
Chunk 7
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n1'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:48.399', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=34, time=2.948456, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=34)), metadata=None)
Chunk 8
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n1'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:49.527', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=40, time=4.076106, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=40)), metadata=None)
Chunk 9
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n1'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:49.681', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=46, time=4.230569, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=46)), metadata=None)
Chunk 10
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:49.850', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=50, time=4.399119, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=50)), metadata=None)
Chunk 11
type: <class 'list'>
ChatResponse(content=[{'type': 'text', 'text': '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20'}], id='0ba2980e-4354-9f06-918e-5210621495db', created_at='2026-05-25 08:41:49.872', type='chat', usage=ChatUsage(input_tokens=27, output_tokens=50, time=4.420958, type='chat', metadata=GenerationUsage(input_tokens=27, output_tokens=50)), metadata=None)
Reasoning¶AgentScope supports reasoning models by providing the async def example_reasoning() -> None: """An example of using the reasoning model.""" model = DashScopeChatModel( model_name="qwen-turbo", api_key=os.environ["DASHSCOPE_API_KEY"], enable_thinking=True, ) res = await model( messages=[ {"role": "user", "content": "Who am I?"}, ], ) last_chunk = None async for chunk in res: last_chunk = chunk print("The final response:") print(last_chunk) asyncio.run(example_reasoning()) The final response:
ChatResponse(content=[{'type': 'thinking', 'thinking': 'Okay, the user asked, "Who am I?" I need to figure out how to respond. First, I should consider that they might be asking about their identity in a philosophical sense, or maybe they\'re looking for a personal connection. But since I\'m an AI, I don\'t have a personal identity. I should clarify that.\n\nI should start by acknowledging that I can\'t know their personal identity. Then, maybe offer to help them explore their own identity. I can ask them to share more about themselves, like their interests or experiences. That way, I can provide a more tailored response. I need to keep the tone friendly and open-ended. Also, make sure not to make assumptions. Let them guide the conversation. Alright, that makes sense.'}, {'type': 'text', 'text': "You are a unique individual with your own thoughts, experiences, and perspectives. While I can't know your specific identity, I can help you explore questions about who you are, what you value, or how you see yourself. If you'd like, you can share more about your interests, goals, or experiences, and I can help you reflect on them. What would you like to discuss? 😊"}], id='0f5ff933-be53-953c-941e-525416a00598', created_at='2026-05-25 08:41:53.829', type='chat', usage=ChatUsage(input_tokens=12, output_tokens=238, time=3.954212, type='chat', metadata=GenerationUsage(input_tokens=12, output_tokens=238)), metadata=None)
Tools API¶Different model providers differ in their tools APIs, e.g. the tools JSON schema, the tool call/response format. To provide a unified interface, AgentScope solves the problem by:
json_schemas = [ { "type": "function", "function": { "name": "google_search", "description": "Search for a query on Google.", "parameters": { "type": "object", "properties": { "query": { "type": "string", "description": "The search query.", }, }, "required": ["query"], }, }, }, ] Further Reading¶Total running time of the script: (0 minutes 9.815 seconds) | ||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |