PYTHON - MISTRAL - API
Simon-Pierre Boucher
2024-09-14

Python Version Jupyter Notebook Mistral Mistral Model Mistral API Mistral

In [1]:
import os
import requests
import json

def call_mistral_api(
    model,
    messages,
    temperature=0.7,
    top_p=1,
    max_tokens=None,
    min_tokens=None,
    stream=False,
    stop=None,
    random_seed=None,
    response_format=None,
    tools=None,
    tool_choice="auto",
    safe_prompt=False
):
    # Get the API key from environment variables
    api_key = os.getenv("MISTRAL_API_KEY")
    
    if not api_key:
        raise ValueError("API key not found in environment variables. Please set MISTRAL_API_KEY.")
    
    url = "https://api.mistral.ai/v1/chat/completions"
    
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    
    body = {
        "model": model,
        "messages": messages,
        "temperature": temperature,
        "top_p": top_p,
        "stream": stream,
        "tool_choice": tool_choice,
        "safe_prompt": safe_prompt
    }
    
    # Add optional parameters if they are not None
    if max_tokens is not None:
        body["max_tokens"] = max_tokens
    if min_tokens is not None:
        body["min_tokens"] = min_tokens
    if stop is not None:
        body["stop"] = stop
    if random_seed is not None:
        body["random_seed"] = random_seed
    if response_format is not None:
        body["response_format"] = response_format
    if tools is not None:
        body["tools"] = tools
    
    response = requests.post(url, headers=headers, data=json.dumps(body))
    
    if response.status_code != 200:
        raise ValueError(f"API request failed: {response.text}")
    
    result = response.json()
    return result
In [2]:
# Define the messages
messages = [
    {"role": "user", "content": "give me a list of gift ideas for my girlfriend"}
]
In [3]:
# Call the function
response = call_mistral_api(
    model="mistral-large-latest",  # Replace with the model ID you wish to use
    messages=messages,
    temperature=0.7,
    top_p=1,
    max_tokens=2024
)

# Display the assistant's response
print(response['choices'][0]['message']['content'])
Here's a list of gift ideas for your girlfriend, covering a range of interests:

1. **Personalized Jewelry**: A necklace or bracelet with her initial or a significant date engraved can be a heartfelt gift.

2. **Experience Gifts**:
   - Cooking or dance classes that you can do together.
   - Concert or theater tickets to see her favorite artist or show.
   - A weekend getaway to a nearby bed and breakfast or scenic location.

3. **Self-Care Items**:
   - High-quality skincare or makeup products.
   - A luxurious bathrobe and slippers set.
   - Aromatherapy diffuser with essential oils.

4. **Books**: If she loves to read, find a highly-rated book in her favorite genre or a beautiful edition of a classic she loves.

5. **Custom Star Map**: A custom star map that shows the alignment of the stars on a significant date and location.

6. **Plants**: If she has a green thumb, a beautiful houseplant along with a nice pot can be a great gift.

7. **Gourmet Food Basket**: Put together a basket with her favorite treats, or high-quality food items like exotic teas, gourmet chocolate, or specialty coffees.

8. **Subscription Services**:
   - A subscription to her favorite magazine.
   - A streaming service she doesn't have yet.
   - A subscription box catering to her interests (e.g., books, beauty products, etc.).

9. **Handwritten Letter**: Sometimes, simple and heartfelt words can mean the most. Write her a love letter expressing your feelings.

10. **Custom Illustration**: Commission an artist to create a custom portrait of the two of you, your pet, or a favorite place.

11. **Memory Jar**: Fill a jar with written notes about happy memories, reasons you love her, or future date ideas.

12. **Technology**: If she's into tech, consider gadgets like wireless earbuds, a Kindle e-reader, or a smart speaker.

13. **Cooking or Baking Class in a Box**: Kits that come with ingredients and instructions for a fun culinary experience at home.

14. **Scrapbook or Photo Album**: Collect your favorite photos and memories together in a beautiful album.

15. **Donation to a Cause**: Make a donation in her name to a charity or cause she cares deeply about.