-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add image processing functionality to OpenAIClient
- Loading branch information
1 parent
3b7387d
commit 85aadd0
Showing
1 changed file
with
30 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,41 @@ | ||
import openai | ||
import openai.resources | ||
|
||
from utils.images import encode_image | ||
|
||
class OpenAIClient: | ||
def __init__(self, api_key): | ||
self.api_key = api_key | ||
openai.api_key = self.api_key | ||
|
||
|
||
def create_completion(self, prompt, model="gpt-4o-mini", max_tokens=50): | ||
print(model) | ||
print(prompt) | ||
print(max_tokens) | ||
|
||
print(chat_completion.choices) | ||
chat_completion = openai.chat.completions.create( | ||
model="gpt-4o-mini", | ||
messages=[{"role": "user", "content": prompt}], | ||
) | ||
message_content = chat_completion.choices[0].message.content | ||
print(message_content) | ||
return chat_completion | ||
return message_content | ||
|
||
def process_image(self): | ||
image_path = './images/puño.jpeg' | ||
base64_image = encode_image(image_path) | ||
response = openai.chat.completions.create( | ||
model="gpt-4o-mini", | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{"type": "text", "text": "En una imagen donde se muestra una mano con los dedos extendidos, ¿qué movimiento esperas que haga la mano? Si la mano se cierra, devuelve 1. Si la mano se abre, devuelve 0. Si es un movimiento de pinzas, devuelve 2."}, | ||
{ | ||
"type": "image_url", | ||
"image_url": { | ||
"url": f"data:image/jpeg;base64,{base64_image}", | ||
}, | ||
}, | ||
], | ||
} | ||
], | ||
max_tokens=300, | ||
) | ||
return response.choices[0].message.content |