Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support claude vision and add examples #123

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/logging/anthropic_messages_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import base64

import anthropic
import httpx

from log10.load import log10


log10(anthropic)

client = anthropic.Anthropic()

image1_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
image1_media_type = "image/jpeg"
image1_data = base64.b64encode(httpx.get(image1_url).content).decode("utf-8")

message = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": image1_media_type,
"data": image1_data,
},
},
{"type": "text", "text": "Describe this image."},
],
}
],
)
print(message)
38 changes: 38 additions & 0 deletions examples/logging/openai_chat_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import base64

import httpx
import openai

from log10.load import log10


log10(openai)

client = openai.OpenAI()


image1_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
image1_media_type = "image/jpeg"
image1_data = base64.b64encode(httpx.get(image1_url).content).decode("utf-8")


response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What are in these image?",
},
{
"type": "image_url",
"image_url": {"url": f"data:{image1_media_type};base64,{image1_data}"},
},
],
}
],
max_tokens=300,
)
print(response.choices[0])
14 changes: 14 additions & 0 deletions log10/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def log_url(res, completionID):
async def log_async(completion_url, func, **kwargs):
global last_completion_response

kwargs = kwargs.copy()
res = None
try:
res = post_request(completion_url)
Expand All @@ -217,6 +218,19 @@ async def log_async(completion_url, func, **kwargs):
if "anthropic" in func.__module__:
if "system" in kwargs:
kwargs["messages"].insert(0, {"role": "system", "content": kwargs["system"]})
if "messages" in kwargs:
for m in kwargs["messages"]:
new_content = []
for c in m.get("content", []):
if c.get("type") == "image":
image_type = c.get("source", {}).get("media_type", "")
image_data = c.get("source", {}).get("data", "")
new_content.append(
{"type": "image_url", "image_url": f"data:{image_type};base64,{image_data}"}
)
else:
new_content.append(c)
m["content"] = new_content

log_row = {
# do we want to also store args?
Expand Down
Loading