Last active
September 24, 2024 13:59
-
-
Save tcapelle/535b3d5b609065984a7e0f0bb16826d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mistralai import Mistral | |
import os | |
import weave | |
weave.init("pixtral") | |
print("1. La Platforme Mistral") | |
api_key = os.environ["MISTRAL_API_KEY"] | |
model = "pixtral-12b-2409" | |
mistral_client = Mistral(api_key=api_key) | |
response = mistral_client.chat.complete( | |
model=model, | |
messages=[ | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": "What’s in this image?" | |
}, | |
{ | |
"type": "image_url", | |
"image_url": "https://tripfixers.com/wp-content/uploads/2019/11/eiffel-tower-with-snow.jpeg" | |
} | |
] | |
} | |
], | |
max_tokens=300 | |
) | |
import base64 | |
import os | |
def encode_image(image_path): | |
"""Encode the image to base64.""" | |
try: | |
with open(image_path, "rb") as image_file: | |
return base64.b64encode(image_file.read()).decode('utf-8') | |
except FileNotFoundError: | |
print(f"Error: The file {image_path} was not found.") | |
return None | |
except Exception as e: # Added general exception handling | |
print(f"Error: {e}") | |
return None | |
image_path = "pug.png" | |
base64_image = encode_image(image_path) | |
# Define the messages for the chat | |
messages = [ | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": "What's in this image?" | |
}, | |
{ | |
"type": "image_url", | |
"image_url": f"data:image/jpeg;base64,{base64_image}" | |
} | |
] | |
} | |
] | |
print("1.5 La plataforma Mistral base64") | |
chat_response = mistral_client.chat.complete( | |
model=model, | |
messages=messages | |
) | |
# print("2. Custom Mistral Server with httpx request") | |
# import httpx | |
# URL = "http://195.242.17.141:8000/v1" | |
# url = URL + "/chat/completions" | |
# headers = {"Content-Type": "application/json", "Authorization": "Bearer token"} | |
# data = { | |
# "model": "mistralai/Pixtral-12B-2409", | |
# "messages": [ | |
# { | |
# "role": "user", | |
# "content": [ | |
# {"type": "text", "text": "Describe this image in a short sentence."}, | |
# { | |
# "type": "image_url", | |
# "image_url": {"url": "https://picsum.photos/id/237/200/300"}, | |
# }, | |
# ], | |
# } | |
# ], | |
# } | |
# response = httpx.post(url, headers=headers, json=data, timeout=20) | |
# print("3. Custom Mistral Server with openai SDK") | |
# import openai | |
# openai_client = openai.OpenAI(base_url=URL) | |
# response = openai_client.chat.completions.create( | |
# model="mistralai/Pixtral-12B-2409", | |
# messages=[ | |
# {"role": "user", "content": "Describe this image in a short sentence."}, | |
# {"role": "user", "content": [{"type": "image_url", "image_url": {"url": "https://picsum.photos/id/237/200/300"}}]} | |
# ], | |
# ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment