Code workflow with Mistral AI Agents
Example of Python code generation using Mistral AI Agents
5 min read
In this post, I will document an example of workflow for Python code generation using Mistral AI Agents.
Overview of Mistral AI Agents
Inspired by the Python Agent Workflow documented in Mistral's official resources, I developed a custom workflow named gen_code_workflow. The objective of this workflow is to automate the generation of Python scripts based on user-provided instructions. A loop is integrated into the workflow to handle script execution and retry in the event of any errors.
The workflow utilizes three Mistral AI agents:
- Python Code Generator Agent: Generates Python scripts based on user inputs.
- Python Code Fixer Agent: Attempts to fix any execution errors in the generated code.
- Judge Agent: Evaluates the Python script against the user’s instructions and provides a score between 0 and 10.
Workflow Arguments
The gen_code_workflow accepts the following arguments:
- User instructions (str)
- Optional: File path to save the resulting Python script (str)
- Optional: Debugging mode (bool)
Workflow Diagram
Diagram generated using Excalidraw integration with Mermaid.
Example Execution
gen_code_workflow(""" Generate a method to search Wikipedia for a specific topic. You will receive the search topic as an argument. If no argument is passed, set a default. The method should return information found on Wikipedia. Use a publicly available Python library to access Wikipedia. Do not limit the number of sentences. Handle errors in case the topic is not found. Document the method with its arguments and return values. ""","test.py", False)
Score: 8.5
Resulting Python Script
import wikipedia
import subprocess
import sys
# Install the wikipedia library if it's not already installed
try:
import wikipedia
except ImportError:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'wikipedia'])
def search_wikipedia(topic='Python programming language'):
"""
Search Wikipedia for a given topic and return the information found.
Args:
topic (str): The topic to search on Wikipedia. Defaults to 'Python programming language'.
Returns:
str: The information found on Wikipedia for the specified topic.
If the topic is not found, returns a user-friendly error message.
"""
try:
# Search Wikipedia for the provided topic
page = wikipedia.page(topic)
return page.content
except wikipedia.exceptions.PageError:
# Handle the case where the topic is not found
return f"The topic '{topic}' was not found on Wikipedia."
except wikipedia.exceptions.DisambiguationError as e:
# Handle disambiguation errors by returning a message with possible options
return f"The topic '{topic}' is ambiguous. Possible options are: {e.options}"
if __name__ == "__main__":
print(search_wikipedia('Artificial Intelligence'))
Useful Links
- Mistral AI Agents Documentation
- Mistral AI Agents API
- The GitHub repository for the Mistral AI Python client library
Enjoyed this post? Found it helpful? Feel free to leave a comment below to share your thoughts or ask questions. A GitHub account is required to join the discussion.