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

LLM Templates - Created vision_gpt_explain_image llm application #964

Merged
merged 9 commits into from
Dec 3, 2023
56 changes: 56 additions & 0 deletions examples/vision_gpt_explain_image/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import agenta as ag
from openai import OpenAI
from typing import List, Dict


client = OpenAI()

ag.init(app_name="explain_image", base_name="app")
aybruhm marked this conversation as resolved.
Show resolved Hide resolved
ag.config.default(
model=ag.MultipleChoiceParam("gpt-4-vision-preview", []),
aybruhm marked this conversation as resolved.
Show resolved Hide resolved
max_tokens=ag.IntParam(300, -1, 4000),
aybruhm marked this conversation as resolved.
Show resolved Hide resolved
)
aybruhm marked this conversation as resolved.
Show resolved Hide resolved


def replace_image_url(
aybruhm marked this conversation as resolved.
Show resolved Hide resolved
messages: List[Dict[str, str]], image_one: str, image_two: str
) -> Dict[str, str]:
new_message = {}
for message in messages:
for key, value in message.items():
if key == "content":
new_content = []
for content in value:
if content["type"] == "image_url":
content["image_url"] = (
{"url": image_two}
if content["image_url"] == image_one
else {"url": image_one}
)
new_content.append(content)
new_message[key] = new_content
else:
new_message[key] = value
return new_message


@ag.entrypoint
def explain(
image_one: ag.FileInputURL,
image_two: ag.FileInputURL,
inputs: ag.DictInput = ag.DictInput(default_keys=["role"]),
aybruhm marked this conversation as resolved.
Show resolved Hide resolved
messages: ag.MessagesInput = ag.MessagesInput(
aybruhm marked this conversation as resolved.
Show resolved Hide resolved
[
{"type": "text", "text": "What are in these image?"},
]
),
) -> str:
messages = [inputs] + [{"content": messages}]
aybruhm marked this conversation as resolved.
Show resolved Hide resolved
new_messages = replace_image_url(messages, image_one, image_two)
max_tokens = ag.config.max_tokens if ag.config.max_tokens != -1 else None
chat_completion = client.chat.completions.create(
model=ag.config.model,
messages=[new_messages],
max_tokens=max_tokens,
)
return chat_completion.choices[0].message.content
2 changes: 2 additions & 0 deletions examples/vision_gpt_explain_image/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
agenta
openai
Loading