The below example demonstrates how to use the Superface API to provide access to external tools in a MinstralAI powered agent.

If you want to run this example for yourself as a Jupyter Notebook, you can download the .ipynb file:

superface_agent_hub_mistralai_example.ipynb

Prerequisities

Install the following dependencies

pip install pandas "mistralai>=0.1.2"

Setup

import json
import random
import requests as r
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
from IPython.display import display, Markdown

# Set a random number of your choice, but don't change it
# once you have run the notebook, otherwise you will create another user.
SUPERFACE_USER_ID_CONSTANT = 123456789

# Use the number to create a unique ID
SUPERFACE_USER_ID = "sfmstrlaidemo|" + str(SUPERFACE_USER_ID_CONSTANT)

# Default URL for Superface
SUPERFACE_BASE_URL = "<https://pod.superface.ai/api/hub>"

# Set the Superface authentication token
SUPERFACE_AUTH_TOKEN="<your-superface-auth-token>"

# Mistral API Key
MISTRAL_API_KEY = "<your-mistral-api-key>"

# A new array for the user, system and LLM messages to be stored
messages = []

MistralAI Setup

Using the MistralAI SDK set up the client and the model

# Setup MistralAI
model = "mistral-large-latest"
client = MistralClient(api_key="MISTRAL_API_KEY")

Helper functions

Below, we have defined two helper functions.

These helpers are for example purposes and you are welcome to build different ways to approach these in whatever manner you choose.

# Helper function to return the tool function descriptors
def get_superface_tools():
  headers = {"Authorization": "Bearer "+ SUPERFACE_AUTH_TOKEN}
  tools = r.get(SUPERFACE_BASE_URL + "/fd", headers=headers)
  return tools.json()

# Helper function to perform the action for all the functions.
# This is the only API call required regardless of what the function is.
def perform_action(tool_name=None, tool_body=None):
  headers = {"Authorization": "Bearer "+ SUPERFACE_AUTH_TOKEN, "x-superface-user-id": SUPERFACE_USER_ID}
  perform = r.post(SUPERFACE_BASE_URL + "/perform/" + tool_name, headers=headers, json=tool_body)
  return json.dumps(perform.json())

Prompt

# User prompt - The weather tool requires no authentication
prompt = "What is the weather in Prague?"