Mistral AI
Mistral AI 是一个基于云端的平台,提供他们自己的大型语言模型,如 Mistral、Mixtral 和 Codestral。
虽然可以通过将base_url
更改为他们的URL,直接使用Mistral AI的API与AutoGen一起使用,但它并不适用于消息传递之间的一些差异,并且由于他们的API比OpenAI的更严格,建议使用Mistral AI客户端类,如本笔记本所示。
你需要一个Mistral.AI账户并创建一个API密钥。详情请参阅他们的网站。
功能
使用此客户端类时,消息会自动调整以满足Mistral AI API的特定要求(例如角色顺序),这些要求比OpenAI的API更加严格。
此外,这个客户端类还支持函数/工具调用,并能根据Mistral AI的API费用(截至2024年6月)正确追踪令牌使用情况和成本。
入门
首先你需要安装 autogen-agentchat~=0.2
包以使用 AutoGen 和 Mistral API 库。
pip install autogen-agentchat[mistral]~=0.2
Mistral提供了许多可供使用的模型,如下所示。请查看模型列表。
请查看下面的示例 OAI_CONFIG_LIST
,显示了如何通过指定 api_type
为 mistral
来使用 Mistral AI 客户端类。
[
{
"model": "gpt-35-turbo",
"api_key": "your OpenAI Key goes here",
},
{
"model": "gpt-4-vision-preview",
"api_key": "your OpenAI Key goes here",
},
{
"model": "dalle",
"api_key": "your OpenAI Key goes here",
},
{
"model": "open-mistral-7b",
"api_key": "your Mistral AI API Key goes here",
"api_type": "mistral"
},
{
"model": "open-mixtral-8x7b",
"api_key": "your Mistral AI API Key goes here",
"api_type": "mistral"
},
{
"model": "open-mixtral-8x22b",
"api_key": "your Mistral AI API Key goes here",
"api_type": "mistral"
},
{
"model": "mistral-small-latest",
"api_key": "your Mistral AI API Key goes here",
"api_type": "mistral"
},
{
"model": "mistral-medium-latest",
"api_key": "your Mistral AI API Key goes here",
"api_type": "mistral"
},
{
"model": "mistral-large-latest",
"api_key": "your Mistral AI API Key goes here",
"api_type": "mistral"
},
{
"model": "codestral-latest",
"api_key": "your Mistral AI API Key goes here",
"api_type": "mistral"
}
]
作为配置文件中 api_key
键和值的替代方案,您可以将环境变量 MISTRAL_API_KEY
设置为您的 Mistral AI 密钥。
Linux/Mac:
export MISTRAL_API_KEY="your_mistral_ai_api_key_here"
Windows:
set MISTRAL_API_KEY=your_mistral_ai_api_key_here
API参数
以下参数可以添加到您的Mistral.AI API配置中。有关它们及其默认值的更多信息,请参阅此链接。
- 温度 (数字 0..1)
- top_p (数字 0..1)
- max_tokens (null, 整数 >= 0)
- random_seed (null, integer)
- safe_prompt(True 或 False)
示例:
[
{
"model": "codestral-latest",
"api_key": "your Mistral AI API Key goes here",
"api_type": "mistral",
"temperature": 0.5,
"top_p": 0.2, # Note: It is recommended to set temperature or top_p but not both.
"max_tokens": 10000,
"safe_prompt": False,
"random_seed": 42
}
]
双代理编码示例
在这个示例中,我们运行了一个双代理聊天,其中包含一个AssistantAgent(主要是一个编码代理)来生成代码,用于计算1到10,000之间的质数数量,然后执行该代码。
我们将使用Mistral的Mixtral 8x22B模型,该模型适合编码。
import os
config_list = [
{
# Let's choose the Mixtral 8x22B model
"model": "open-mixtral-8x22b",
# Provide your Mistral AI API key here or put it into the MISTRAL_API_KEY environment variable.
"api_key": os.environ.get("MISTRAL_API_KEY"),
# We specify the API Type as 'mistral' so it uses the Mistral AI client class
"api_type": "mistral",
}
]
重要的是,我们已经调整了系统消息,以便模型不会返回终止关键字,我们已将其更改为 FINISH,与代码块一起。
from pathlib import Path
from autogen import AssistantAgent, UserProxyAgent
from autogen.coding import LocalCommandLineCodeExecutor
# Setting up the code executor
workdir = Path("coding")
workdir.mkdir(exist_ok=True)
code_executor = LocalCommandLineCodeExecutor(work_dir=workdir)
# Setting up the agents
# The UserProxyAgent will execute the code that the AssistantAgent provides
user_proxy_agent = UserProxyAgent(
name="User",
code_execution_config={"executor": code_executor},
is_termination_msg=lambda msg: "FINISH" in msg.get("content"),
)
system_message = """You are a helpful AI assistant who writes code and the user executes it.
Solve tasks using your coding and language skills.
In the following cases, suggest python code (in a python coding block) for the user to execute.
Solve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.
When using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.
Don't include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use 'print' function for the output when relevant. Check the execution result returned by the user.
If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.
When you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible.
IMPORTANT: Wait for the user to execute your code and then you can reply with the word "FINISH". DO NOT OUTPUT "FINISH" after your code block."""
# The AssistantAgent, using Mistral AI's model, will take the coding request and return code
assistant_agent = AssistantAgent(
name="Mistral Assistant",
system_message=system_message,
llm_config={"config_list": config_list},
)
# Start the chat, with the UserProxyAgent asking the AssistantAgent the message
chat_result = user_proxy_agent.initiate_chat(
assistant_agent,
message="Provide code to count the number of prime numbers from 1 to 10000.",
)
User (to Mistral Assistant):
Provide code to count the number of prime numbers from 1 to 10000.
--------------------------------------------------------------------------------
Mistral
To solve this task, I will write a Python function that checks if a number is prime or not. Then, I will iterate through the numbers from 1 to 10000 and count the prime numbers. Here's the plan:
1. Write a function `is_prime(n)` that checks if a number `n` is prime.
2. Initialize a variable `prime_count` to 0.
3. Iterate through numbers from 1 to 10000 using a for loop.
4. For each number, call the `is_prime(n)` function.
5. If the function returns True, increment the `prime_count` by 1.
6. Finally, print the `prime_count`.
Here's the code that implements this plan:
```python
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
prime_count = 0
for num in range(1, 10001):
if is_prime(num):
prime_count += 1
print(prime_count)
```
This code will count the number of prime numbers from 1 to 10000 and print the result. Please execute the code and let me know the output.
```python
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
prime_count = 0
for num in range(1, 10001):
if is_prime(num):
prime_count += 1
prime_count
```
--------------------------------------------------------------------------------
>>>>>>>> NO HUMAN INPUT RECEIVED.
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING 2 CODE BLOCKS (inferred languages are [python, python])...
User (to Mistral Assistant):
exitcode: 0 (execution succeeded)
Code output: 1229
--------------------------------------------------------------------------------
Mistral
The code executed successfully and the output is 1229.
This means there are 1229 prime numbers from 1 to 10000.
The code defines a function `is_prime(n)` that checks if a number `n` is prime or not. Then, it initializes a variable `prime_count` to 0. It iterates through numbers from 1 to 10000 using a for loop. For each number, it calls the `is_prime(n)` function. If the function returns True, indicating that the number is prime, it increments the `prime_count` by 1. Finally, it prints the `prime_count`.
This code efficiently checks for prime numbers and accurately counts the number of prime numbers from 1 to 10000.
FINISH
--------------------------------------------------------------------------------
>>>>>>>> NO HUMAN INPUT RECEIVED.
工具调用示例
在这个示例中,我们不会编写代码,而是让两个代理使用工具调用进行移动,彼此下棋。
我们将为这项具有挑战性的任务更改模型为Mistral AI的大模型。
config_list = [
{
# Let's choose the Mistral AI's largest model which is better at Chess than the Mixtral model
"model": "mistral-large-latest",
"api_key": os.environ.get("MISTRAL_API_KEY"),
"api_type": "mistral",
}
]
首先通过运行以下命令安装chess
包:
! pip install chess
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: chess in /home/autogen/.local/lib/python3.11/site-packages (1.10.0)
编写做出移动的函数。
import random
import chess
import chess.svg
from IPython.display import display
from typing_extensions import Annotated
board = chess.Board()
def make_move() -> Annotated[str, "A move in UCI format"]:
moves = list(board.legal_moves)
move = random.choice(moves)
board.push(move)
# Display the board.
display(chess.svg.board(board, size=400))
return str(move)
让我们创建代理。我们有三种不同的代理: -
player_white
是执白棋的代理。 - player_black
是
执黑棋的代理。 - board_proxy
是在棋盘上
移动棋子的代理。
from autogen import ConversableAgent, register_function
player_white = ConversableAgent(
name="Player White",
system_message="You are a chess player and you play as white. " "Always call make_move() to make a move",
llm_config={"config_list": config_list, "cache_seed": None},
)
player_black = ConversableAgent(
name="Player Black",
system_message="You are a chess player and you play as black. " "Always call make_move() to make a move",
llm_config={"config_list": config_list, "cache_seed": None},
)
board_proxy = ConversableAgent(
name="Board Proxy",
llm_config=False,
# The board proxy will only respond to the make_move function.
is_termination_msg=lambda msg: "tool_calls" not in msg,
)
为代理注册工具。有关更多信息,请参阅工具使用教程章节。
register_function(
make_move,
caller=player_white,
executor=board_proxy,
name="make_move",
description="Make a move.",
)
register_function(
make_move,
caller=player_black,
executor=board_proxy,
name="make_move",
description="Make a move.",
)
/home/autogen/autogen/autogen/agentchat/conversable_agent.py:2408: UserWarning: Function 'make_move' is being overridden.
warnings.warn(f"Function '{name}' is being overridden.", UserWarning)
为玩家代理注册嵌套聊天。嵌套聊天允许每个玩家代理与棋盘代理聊天以做出移动,然后再与其他玩家代理进行通信。更多信息请参阅嵌套聊天教程章节。
player_white.register_nested_chats(
trigger=player_black,
chat_queue=[
{
"sender": board_proxy,
"recipient": player_white,
}
],
)
player_black.register_nested_chats(
trigger=player_white,
chat_queue=[
{
"sender": board_proxy,
"recipient": player_black,
}
],
)
清空棋盘并开始棋局。
# Clear the board.
board = chess.Board()
chat_result = player_white.initiate_chat(
player_black,
message="Let's play chess! Your move.",
max_turns=4,
)
Player White (to Player Black):
Let's play chess! Your move.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
********************************************************************************
Starting a new chat....
********************************************************************************
Board Proxy (to Player Black):
Let's play chess! Your move.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player Black (to Board Proxy):
***** Suggested tool call (AcS1aX4Rl): make_move *****
Arguments:
{}
******************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING FUNCTION make_move...
Board Proxy (to Player Black):
Board Proxy (to Player Black):
***** Response from calling tool (AcS1aX4Rl) *****
g1h3
**************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player Black (to Board Proxy):
I have made my move. It's g1h3. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> NO HUMAN INPUT RECEIVED.
Player Black (to Player White):
I have made my move. It's g1h3. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
********************************************************************************
Starting a new chat....
********************************************************************************
Board Proxy (to Player White):
I have made my move. It's g1h3. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player White (to Board Proxy):
***** Suggested tool call (tWVXVAujE): make_move *****
Arguments:
{}
******************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING FUNCTION make_move...
Board Proxy (to Player White):
Board Proxy (to Player White):
***** Response from calling tool (tWVXVAujE) *****
d7d5
**************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player White (to Board Proxy):
I have made my move. It's d7d5. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> NO HUMAN INPUT RECEIVED.
Player White (to Player Black):
I have made my move. It's d7d5. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
********************************************************************************
Starting a new chat....
********************************************************************************
Board Proxy (to Player Black):
I have made my move. It's d7d5. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player Black (to Board Proxy):
***** Suggested tool call (ZOfvRz0B1): make_move *****
Arguments:
{}
******************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING FUNCTION make_move...
Board Proxy (to Player Black):
Board Proxy (to Player Black):
***** Response from calling tool (ZOfvRz0B1) *****
b1c3
**************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player Black (to Board Proxy):
I have made my move. It's b1c3. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> NO HUMAN INPUT RECEIVED.
Player Black (to Player White):
I have made my move. It's b1c3. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
********************************************************************************
Starting a new chat....
********************************************************************************
Board Proxy (to Player White):
I have made my move. It's b1c3. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player White (to Board Proxy):
***** Suggested tool call (LovRpi6Pq): make_move *****
Arguments:
{}
******************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING FUNCTION make_move...
Board Proxy (to Player White):
Board Proxy (to Player White):
***** Response from calling tool (LovRpi6Pq) *****
c8g4
**************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player White (to Board Proxy):
I have made my move. It's c8g4. Your turn.
--------------------------------------------------------------------------------
Player White (to Player Black):
I have made my move. It's c8g4. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
********************************************************************************
Starting a new chat....
********************************************************************************
Board Proxy (to Player Black):
I have made my move. It's c8g4. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player Black (to Board Proxy):
***** Suggested tool call (RfhC8brG7): make_move *****
Arguments:
{}
******************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING FUNCTION make_move...
Board Proxy (to Player Black):
Board Proxy (to Player Black):
***** Response from calling tool (RfhC8brG7) *****
a1b1
**************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player Black (to Board Proxy):
I have made my move. It's a1b1. Your turn.
--------------------------------------------------------------------------------
Player Black (to Player White):
I have made my move. It's a1b1. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
********************************************************************************
Starting a new chat....
********************************************************************************
Board Proxy (to Player White):
I have made my move. It's a1b1. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player White (to Board Proxy):
***** Suggested tool call (6aVW1t0lm): make_move *****
Arguments:
{}
******************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING FUNCTION make_move...
Board Proxy (to Player White):
Board Proxy (to Player White):
***** Response from calling tool (6aVW1t0lm) *****
a7a6
**************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player White (to Board Proxy):
I have made my move. It's a7a6. Your turn.
--------------------------------------------------------------------------------
Player White (to Player Black):
I have made my move. It's a7a6. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
********************************************************************************
Starting a new chat....
********************************************************************************
Board Proxy (to Player Black):
I have made my move. It's a7a6. Your turn.
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player Black (to Board Proxy):
***** Suggested tool call (kPTEInlLR): make_move *****
Arguments:
{}
******************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING FUNCTION make_move...
Board Proxy (to Player Black):
Board Proxy (to Player Black):
***** Response from calling tool (kPTEInlLR) *****
h3f4
**************************************************
--------------------------------------------------------------------------------
>>>>>>>> USING AUTO REPLY...
Player Black (to Board Proxy):
I have made my move. It's h3f4. Your turn.
--------------------------------------------------------------------------------
Player Black (to Player White):
I have made my move. It's h3f4. Your turn.
--------------------------------------------------------------------------------
[runtime logging] log_function_use: autogen logger is None
[runtime logging] log_function_use: autogen logger is None
[runtime logging] log_function_use: autogen logger is None
[runtime logging] log_function_use: autogen logger is None
[runtime logging] log_function_use: autogen logger is None
[runtime logging] log_function_use: autogen logger is None
[runtime logging] log_function_use: autogen logger is None