|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Examples of MiniMax chat model calls. |
| 3 | +
|
| 4 | +The MiniMax M-series chat models (e.g. ``MiniMax-M3``) are exposed via |
| 5 | +MiniMax's officially recommended Anthropic-compatible API, so the |
| 6 | +controls and outputs match |
| 7 | +``scripts/model_examples/anthropic_call.py``. |
| 8 | +""" |
| 9 | + |
| 10 | +import asyncio |
| 11 | +import json |
| 12 | +import os |
| 13 | + |
| 14 | +from pydantic import BaseModel, Field |
| 15 | + |
| 16 | +from _utils import stream_and_collect |
| 17 | +from agentscope.credential import MiniMaxCredential |
| 18 | +from agentscope.message import ( |
| 19 | + Msg, |
| 20 | + TextBlock, |
| 21 | + ToolCallBlock, |
| 22 | + ToolResultBlock, |
| 23 | + ToolResultState, |
| 24 | +) |
| 25 | +from agentscope.model import MiniMaxChatModel |
| 26 | +from agentscope.tool import Toolkit, ToolChoice, FunctionTool |
| 27 | + |
| 28 | + |
| 29 | +# --------------------------------------------------------------------------- |
| 30 | +# Example 1: Simple user message (streaming, with extended thinking) |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | + |
| 33 | + |
| 34 | +async def example_simple_call() -> None: |
| 35 | + """Call MiniMax-M3 with a simple text message and extended thinking.""" |
| 36 | + model = MiniMaxChatModel( |
| 37 | + credential=MiniMaxCredential( |
| 38 | + api_key=os.environ["MINIMAX_API_KEY"], |
| 39 | + ), |
| 40 | + model="MiniMax-M3", |
| 41 | + stream=True, |
| 42 | + parameters=MiniMaxChatModel.Parameters( |
| 43 | + thinking_enable=True, |
| 44 | + ), |
| 45 | + ) |
| 46 | + |
| 47 | + msgs = [ |
| 48 | + Msg( |
| 49 | + name="user", |
| 50 | + content=[TextBlock(text="What is 1 + 1? Answer briefly.")], |
| 51 | + role="user", |
| 52 | + ), |
| 53 | + ] |
| 54 | + |
| 55 | + print("=== Simple Call ===") |
| 56 | + await stream_and_collect(await model(msgs)) |
| 57 | + |
| 58 | + |
| 59 | +# --------------------------------------------------------------------------- |
| 60 | +# Example 2: Tool calling (streaming) |
| 61 | +# --------------------------------------------------------------------------- |
| 62 | + |
| 63 | + |
| 64 | +def get_weather(city: str) -> str: |
| 65 | + """Get the current weather for a city. |
| 66 | +
|
| 67 | + Args: |
| 68 | + city: The city name to query the weather for. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + A description of the current weather. |
| 72 | + """ |
| 73 | + return f"The weather in {city} is sunny and 25°C." |
| 74 | + |
| 75 | + |
| 76 | +async def example_tool_call() -> None: |
| 77 | + """Call MiniMax-M3 with tool calling enabled.""" |
| 78 | + toolkit = Toolkit(tools=[FunctionTool(get_weather)]) |
| 79 | + tools = await toolkit.get_tool_schemas() |
| 80 | + |
| 81 | + model = MiniMaxChatModel( |
| 82 | + credential=MiniMaxCredential( |
| 83 | + api_key=os.environ["MINIMAX_API_KEY"], |
| 84 | + ), |
| 85 | + model="MiniMax-M3", |
| 86 | + stream=True, |
| 87 | + parameters=MiniMaxChatModel.Parameters( |
| 88 | + thinking_enable=True, |
| 89 | + ), |
| 90 | + ) |
| 91 | + |
| 92 | + msgs = [ |
| 93 | + Msg( |
| 94 | + name="user", |
| 95 | + content=[TextBlock(text="What is the weather in Shanghai?")], |
| 96 | + role="user", |
| 97 | + ), |
| 98 | + ] |
| 99 | + |
| 100 | + print("=== Tool Call - Round 1 ===") |
| 101 | + response = await stream_and_collect( |
| 102 | + await model(msgs, tools=tools, tool_choice=ToolChoice(mode="auto")), |
| 103 | + ) |
| 104 | + print(response) |
| 105 | + |
| 106 | + tool_calls = [b for b in response.content if isinstance(b, ToolCallBlock)] |
| 107 | + if tool_calls: |
| 108 | + tool_result_blocks = [] |
| 109 | + for tool_call in tool_calls: |
| 110 | + args = json.loads(tool_call.input) |
| 111 | + result = get_weather(**args) |
| 112 | + tool_result_blocks.append( |
| 113 | + ToolResultBlock( |
| 114 | + id=tool_call.id, |
| 115 | + name=tool_call.name, |
| 116 | + output=result, |
| 117 | + state=ToolResultState.SUCCESS, |
| 118 | + ), |
| 119 | + ) |
| 120 | + |
| 121 | + assistant_msg = Msg( |
| 122 | + name="assistant", |
| 123 | + content=response.content, |
| 124 | + role="assistant", |
| 125 | + ) |
| 126 | + tool_result_msg = Msg( |
| 127 | + name="tool", |
| 128 | + content=tool_result_blocks, |
| 129 | + role="assistant", |
| 130 | + ) |
| 131 | + msgs = msgs + [assistant_msg, tool_result_msg] |
| 132 | + |
| 133 | + print("=== Tool Call - Round 2 (Final) ===") |
| 134 | + await stream_and_collect(await model(msgs)) |
| 135 | + |
| 136 | + |
| 137 | +# --------------------------------------------------------------------------- |
| 138 | +# Example 3: Structured output |
| 139 | +# --------------------------------------------------------------------------- |
| 140 | + |
| 141 | + |
| 142 | +class MathSolution(BaseModel): |
| 143 | + """Structured solution to a math problem.""" |
| 144 | + |
| 145 | + problem: str = Field(description="The original problem statement") |
| 146 | + answer: float = Field(description="The final numeric answer") |
| 147 | + steps: list[str] = Field( |
| 148 | + description="Step-by-step reasoning leading to the answer", |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +async def example_structured_output() -> None: |
| 153 | + """Call MiniMax-M3 and force a structured (JSON) output.""" |
| 154 | + model = MiniMaxChatModel( |
| 155 | + credential=MiniMaxCredential( |
| 156 | + api_key=os.environ["MINIMAX_API_KEY"], |
| 157 | + ), |
| 158 | + model="MiniMax-M3", |
| 159 | + stream=True, |
| 160 | + parameters=MiniMaxChatModel.Parameters( |
| 161 | + thinking_enable=True, |
| 162 | + ), |
| 163 | + ) |
| 164 | + |
| 165 | + msgs = [ |
| 166 | + Msg( |
| 167 | + name="user", |
| 168 | + content=[ |
| 169 | + TextBlock( |
| 170 | + text=( |
| 171 | + "Solve this: A train travels at 60 km/h for " |
| 172 | + "2.5 hours. How far does it travel in km?" |
| 173 | + ), |
| 174 | + ), |
| 175 | + ], |
| 176 | + role="user", |
| 177 | + ), |
| 178 | + ] |
| 179 | + |
| 180 | + print("=== Structured Output ===") |
| 181 | + response = await model.generate_structured_output( |
| 182 | + msgs, |
| 183 | + structured_model=MathSolution, |
| 184 | + ) |
| 185 | + print(response.content) |
| 186 | + |
| 187 | + |
| 188 | +if __name__ == "__main__": |
| 189 | + asyncio.run(example_simple_call()) |
| 190 | + asyncio.run(example_tool_call()) |
| 191 | + asyncio.run(example_structured_output()) |
0 commit comments