MarkTechPost@AI 06月03日 15:25
Hands-On Guide: Getting started with Mistral Agents API
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何使用Mistral Agents API构建一个数学求解智能体。该API支持多种多模态模型,包括文本和图像交互,并具备对话记忆功能,方便智能体记住上下文。通过内置的工具,如代码执行、网页浏览等,智能体能够高效地解决数学问题。文章详细阐述了设置依赖、创建智能体、运行智能体等步骤,并提供了代码示例,帮助开发者快速上手。最终,用户可以利用该智能体解决方程,并绘制函数图像。

💻 Mistral Agents API支持多模态模型,涵盖文本和图像,并具备对话记忆功能,使智能体能够记住上下文信息,实现更自然的交互。

🛠️ 该API内置了代码执行、网页浏览、图像生成等工具,使得智能体能够执行Python代码来解决数学问题,提供了强大的功能支持。

⚙️ 构建数学求解智能体的关键步骤包括:安装Mistral库、加载API密钥、创建Mistral客户端和Agent。Agent使用mistral-medium-2505模型,并配置code_interpreter工具,用于执行代码。

💬 通过启动对话并向智能体提问,例如求解二次方程,用户可以获得详细的解答和代码执行结果。API支持继续对话,保持上下文,从而绘制函数图像。

The Mistral Agents API enables developers to create smart, modular agents equipped with a wide range of capabilities. Key features include:

In this guide, we’ll demonstrate how to build a basic math-solving agent using the Mistral Agents API. Our agent will use the code interpreter tool to handle and solve math problems programmatically.

Step 1: Setting up dependencies

Installing the Mistral library

Loading the Mistral API Key

You can get an API key from https://console.mistral.ai/api-keys

from getpass import getpassapiKey = getpass('Enter Mistral API Key: ')

Step 2: Creating the Mistral client and Agent

The following code creates a custom math agent using the Mistral Agents API. The agent, named Math Helper, is configured to solve mathematical problems, evaluate expressions, and explain concepts. It uses the mistral-medium-2505 model along with Mistral’s built-in code_interpreter tool, allowing it to run Python code when needed. The agent is initialized with clear instructions and tuned with specific completion parameters to ensure accurate and focused responses.

from mistralai import Mistralclient = Mistral(apiKey)math_agent = client.beta.agents.create(    model="mistral-medium-2505",    description="An agent that solves math problems and evaluates expressions.",    name="Math Helper",    instructions="You are a helpful math assistant. You can explain concepts, solve equations, and evaluate math expressions using the code interpreter.",    tools=[{"type": "code_interpreter"}],    completion_args={        "temperature": 0.2,        "top_p": 0.9    })

Step 3: Running the Agent

Initializing the conversation

The following code initiates a new conversation with the math_agent, asking it to solve the quadratic equation 2x² + 3x – 2 = 0. The start() method sends the input query to the agent, which uses the specified model and tools (like the code interpreter) to generate a response. The result, including the assistant’s explanation and code execution, is stored in the response variable.

response = client.beta.conversations.start(    agent_id=math_agent.id, inputs="Solve the quadratic equation 2x² + 3x - 2 = 0", #store=False)print(response)

You can use the following code to get the final output and the executed code:

response.outputs[2].content
print(response.outputs[1].info['code'])

Plotting the results of the executed code

response = client.beta.conversations.append(    conversation_id=response.conversation_id, inputs="Plot the function f(x) = 2x² + 3x - 2")

Continuing the conversation using conversations.append ensures that the agent retains the context and builds upon the previous interactions, allowing for a more natural and coherent dialogue.

file_id = response.outputs[2].content[0].file_idfile_bytes = client.files.download(file_id=file_id).read()with open(f"image_generated.png", "wb") as file:    file.write(file_bytes)

This code will download the generated image as image_generated.png in the current directory. We can display the the same using the following code

from IPython.display import Image, displayimage_path = "image_generated.png"display(Image(filename=image_path))

Check out the Notebook here. All credit for this research goes to the researchers of this project. Also, feel free to follow us on Twitter and don’t forget to join our 95k+ ML SubReddit and Subscribe to our Newsletter.

The post Hands-On Guide: Getting started with Mistral Agents API appeared first on MarkTechPost.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Mistral Agents API AI Agent 数学求解 代码执行
相关文章