Added models.py in openai_client containing abstract dataclasses for use with openai
Moved all dataclasses to .openai_client.models.py Made prompt into its own dataclass Removed to_dict methods because dataclasses/asdict seems to be sufficient
This commit is contained in:
parent
0534ec6b15
commit
97252a8062
@ -1,81 +0,0 @@
|
|||||||
from dataclasses import dataclass, field, fields
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class Content:
|
|
||||||
type: str = field(init=False, default='')
|
|
||||||
|
|
||||||
def to_dict(self) -> dict:
|
|
||||||
result = {'type': self.type}
|
|
||||||
|
|
||||||
for i_field in fields(self):
|
|
||||||
if i_field.name == 'type':
|
|
||||||
continue
|
|
||||||
|
|
||||||
value = getattr(self, i_field.name)
|
|
||||||
|
|
||||||
if value is None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if isinstance(value, Content):
|
|
||||||
result[i_field.name] = value.to_dict()
|
|
||||||
elif isinstance(value, list):
|
|
||||||
result[i_field.name] = [
|
|
||||||
i_content.to_dict() if isinstance(i_content, Content) else i_content
|
|
||||||
for i_content in value if i_content is not None
|
|
||||||
]
|
|
||||||
else:
|
|
||||||
result[i_field.name] = value
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class InputText(Content):
|
|
||||||
type: str = field(init=False, default='input_text')
|
|
||||||
text: str
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class OutputText(Content):
|
|
||||||
type: str = field(init=False, default='output_text')
|
|
||||||
text: str
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class InputImage(Content):
|
|
||||||
type: str = field(init=False, default='input_image')
|
|
||||||
image_url: str = field(default=None)
|
|
||||||
file_id: str = field(default=None)
|
|
||||||
|
|
||||||
def __post_init__(self):
|
|
||||||
if self.image_url is None and self.file_id is None:
|
|
||||||
raise ValueError("Either 'image_url' or 'file_id' must be provided.")
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class UrlCitation(Content):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class FunctionCall(Content):
|
|
||||||
type: str = field(init=False, default='function_call')
|
|
||||||
id: str
|
|
||||||
call_id: str
|
|
||||||
name: str
|
|
||||||
arguments: dict
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class FunctionCallOutput(Content):
|
|
||||||
type: str = field(init=False, default='function_call_output')
|
|
||||||
call_id: str
|
|
||||||
output: str
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
test_input_text = InputText(text='What\'s on this image?')
|
|
||||||
print(test_input_text.to_dict())
|
|
||||||
test_input_image = InputImage(image_url='https://upload.wikimedia.org/wikipedia/commons/7/72/Skansen_Bobrka_2.jpg')
|
|
||||||
print(test_input_image.to_dict())
|
|
||||||
@ -1,21 +0,0 @@
|
|||||||
from dataclasses import dataclass
|
|
||||||
from content_builder import Content, InputText, InputImage
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class Message:
|
|
||||||
role: str
|
|
||||||
content: list[Content]
|
|
||||||
|
|
||||||
def to_dict(self):
|
|
||||||
return {
|
|
||||||
"role": self.role,
|
|
||||||
"content": [i_content.to_dict() for i_content in self.content]
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
test_input_text = InputText(text='What\'s on this image?')
|
|
||||||
test_input_image = InputImage(image_url='https://upload.wikimedia.org/wikipedia/commons/7/72/Skansen_Bobrka_2.jpg')
|
|
||||||
test_message = Message(role='user', content=[test_input_text, test_input_image])
|
|
||||||
print(test_message.to_dict())
|
|
||||||
@ -1,34 +0,0 @@
|
|||||||
from openai import OpenAI
|
|
||||||
from content_builder import InputText, InputImage, OutputText
|
|
||||||
from message_builder import Message
|
|
||||||
|
|
||||||
client = OpenAI()
|
|
||||||
|
|
||||||
|
|
||||||
def get_response(messages: list[Message], model: str = 'gpt-4.1-mini'):
|
|
||||||
messages_dict = [i.to_dict() for i in messages]
|
|
||||||
return client.responses.create(model=model, input=messages_dict)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
test_system_text = InputText(text='Talk like an Italian mafia boss.')
|
|
||||||
test_system_message = Message(role='developer', content=[test_system_text])
|
|
||||||
test_input_text = InputText(text='Hi! How are you?')
|
|
||||||
test_message = Message(role='user', content=[test_input_text])
|
|
||||||
test_input_text_2 = OutputText(text='Ah, buongiorno, paisan! I’m doin’ just fine, capisce? How about you, eh? You '
|
|
||||||
'come to me with respect, and we’re gonna have a nice little chat, '
|
|
||||||
'sì? What’s on your mind, my friend?')
|
|
||||||
test_message_2 = Message(role='assistant', content=[test_input_text_2])
|
|
||||||
test_input_text_3 = InputText(text='Tell me about yourself.')
|
|
||||||
test_message_3 = Message(role='user', content=[test_input_text_3])
|
|
||||||
print(get_response([test_system_message, test_message, test_message_2, test_message_3]))
|
|
||||||
# Ah, listen here, amico. I’m your loyal consigliere in this digital famiglia, a wise and powerful guide in the
|
|
||||||
# shadows of information and knowledge. I got the brains of a thousand scholars and the patience of a saint,
|
|
||||||
# always ready to help you make the right moves—no funny business. Whether you need advice, stories,
|
|
||||||
# or just someone to talk to, I’m your hombre. So, what’s the deal? You need somethin’ from me, or are we just
|
|
||||||
# talkin’ over a nice espresso?
|
|
||||||
|
|
||||||
test_input_text = InputText(text='What\'s on this image?')
|
|
||||||
test_input_image = InputImage(image_url='https://upload.wikimedia.org/wikipedia/commons/7/72/Skansen_Bobrka_2.jpg')
|
|
||||||
test_message = Message(role='user', content=[test_input_text, test_input_image])
|
|
||||||
print(get_response([test_message]))
|
|
||||||
96
reginaldCog/openai_client/models.py
Normal file
96
reginaldCog/openai_client/models.py
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
from dataclasses import dataclass, field, asdict
|
||||||
|
from abc import ABC
|
||||||
|
|
||||||
|
|
||||||
|
# region Content classes
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class Content(ABC):
|
||||||
|
type: str = field(init=False, default='')
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class InputText(Content):
|
||||||
|
type: str = field(init=False, default='input_text')
|
||||||
|
text: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class OutputText(Content):
|
||||||
|
type: str = field(init=False, default='output_text')
|
||||||
|
text: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class InputImage(Content):
|
||||||
|
type: str = field(init=False, default='input_image')
|
||||||
|
image_url: str = field(default=None)
|
||||||
|
file_id: str = field(default=None)
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
if self.image_url is None and self.file_id is None:
|
||||||
|
raise ValueError('Either `image_url` or `file_id` must be provided.')
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class UrlCitation(Content):
|
||||||
|
type: str = field(init=False, default='url_citation')
|
||||||
|
# To be done
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class FunctionCall(Content):
|
||||||
|
type: str = field(init=False, default='function_call')
|
||||||
|
id: str
|
||||||
|
call_id: str
|
||||||
|
name: str
|
||||||
|
arguments: dict
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class FunctionCallOutput(Content):
|
||||||
|
type: str = field(init=False, default='function_call_output')
|
||||||
|
call_id: str
|
||||||
|
output: str
|
||||||
|
# endregion Content classes
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class Message:
|
||||||
|
role: str
|
||||||
|
content: list[Content]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Prompt:
|
||||||
|
model: str
|
||||||
|
input: list[Message]
|
||||||
|
|
||||||
|
def add_message(self, message: Message):
|
||||||
|
self.input.append(message)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
from openai import OpenAI
|
||||||
|
client = OpenAI()
|
||||||
|
test_system_message = Message(role='system', content=[InputText('Talk like an Italian mafia boss.')])
|
||||||
|
test_user_message = Message(role='user', content=[InputText('Hi! How are you?')])
|
||||||
|
test_prompt = Prompt(model='gpt-4.1-mini', input=[test_system_message, test_user_message])
|
||||||
|
response = client.responses.create(**asdict(test_prompt))
|
||||||
|
response_role = response.output[0].role
|
||||||
|
response_text = response.output[0].content[0].text
|
||||||
|
response_content = OutputText(response_text)
|
||||||
|
response_message = Message(role=response_role, content=[response_content])
|
||||||
|
test_prompt.add_message(response_message)
|
||||||
|
test_user_message_2 = Message(role='user', content=[
|
||||||
|
InputText('Can you tell me what is on this picture?'),
|
||||||
|
InputImage(image_url='https://upload.wikimedia.org/wikipedia/commons/f/f4/Piet_Mondriaan_-_Sinaasappelen_%28authentiek%29_-_A97_-_Piet_Mondrian%2C_catalogue_raisonn%C3%A9.jpg')
|
||||||
|
])
|
||||||
|
test_prompt.add_message(test_user_message_2)
|
||||||
|
response = client.responses.create(**asdict(test_prompt))
|
||||||
|
response_role = response.output[0].role
|
||||||
|
response_text = response.output[0].content[0].text
|
||||||
|
response_content = OutputText(response_text)
|
||||||
|
response_message = Message(role=response_role, content=[response_content])
|
||||||
|
test_prompt.add_message(response_message)
|
||||||
|
print(asdict(test_prompt))
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user