Experimented with more convenient (although, less explicit) way of passing data into abstract dataclasses.
This commit is contained in:
parent
97252a8062
commit
89b98fc5af
@ -1,4 +1,4 @@
|
||||
from dataclasses import dataclass, field, asdict
|
||||
from dataclasses import dataclass, field, asdict, fields
|
||||
from abc import ABC
|
||||
|
||||
|
||||
@ -69,6 +69,10 @@ class Prompt:
|
||||
self.input.append(message)
|
||||
|
||||
|
||||
def filter_fields(data: dict, filter_by_class: object) -> dict:
|
||||
return {k: v for k, v in data.items() if k in tuple(f.name for f in fields(filter_by_class))}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from openai import OpenAI
|
||||
client = OpenAI()
|
||||
@ -76,10 +80,9 @@ if __name__ == '__main__':
|
||||
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])
|
||||
response_output = response.to_dict().get('output', [''])[0]
|
||||
response_message = Message(**filter_fields(response_output, Message))
|
||||
print(response.to_dict().get('output'))
|
||||
test_prompt.add_message(response_message)
|
||||
test_user_message_2 = Message(role='user', content=[
|
||||
InputText('Can you tell me what is on this picture?'),
|
||||
@ -93,4 +96,3 @@ if __name__ == '__main__':
|
||||
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