Added OpenAI response -> OpenAIMessage object adapter
This commit is contained in:
parent
7dc1d485b1
commit
a5bfccb19a
@ -44,6 +44,10 @@ class OpenAIContentBuilder(ILLMContentBuilder):
|
|||||||
self.content.content_items.append(item)
|
self.content.content_items.append(item)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def add_from_dict(self, item: dict):
|
||||||
|
self.content.content_items.append(item)
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
class ILLMMessage(ABC):
|
class ILLMMessage(ABC):
|
||||||
pass
|
pass
|
||||||
@ -123,6 +127,33 @@ class OpenAIClient(ILLMClient):
|
|||||||
return self.client.responses.create(**response_input)
|
return self.client.responses.create(**response_input)
|
||||||
|
|
||||||
|
|
||||||
|
class IMessageAdapter(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def to_message(self) -> ILLMMessage:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class OpenAIResponseAdapter(IMessageAdapter):
|
||||||
|
def __init__(self, response):
|
||||||
|
self.response = response
|
||||||
|
self.response_output = response.output[0]
|
||||||
|
|
||||||
|
def to_message(self) -> OpenAIMessage:
|
||||||
|
content = OpenAIContent()
|
||||||
|
content_builder = OpenAIContentBuilder(content)
|
||||||
|
message = OpenAIMessage()
|
||||||
|
message_builder = OpenAIMessageBuilder(message)
|
||||||
|
|
||||||
|
message_builder.set_role(self.response_output.role)\
|
||||||
|
.set_content(content)
|
||||||
|
|
||||||
|
for i_content_item in self.response_output.content:
|
||||||
|
item = i_content_item.to_dict()
|
||||||
|
content_builder.add_from_dict(item)
|
||||||
|
|
||||||
|
return message
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
dev_message_text = "You are a butler named Reginald. Give your answers in a refined manners of the English butler."
|
dev_message_text = "You are a butler named Reginald. Give your answers in a refined manners of the English butler."
|
||||||
user_message_text = "Hi! Tell me about yourself!"
|
user_message_text = "Hi! Tell me about yourself!"
|
||||||
@ -147,8 +178,13 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
# Create client and get response
|
# Create client and get response
|
||||||
client = OpenAIClient()
|
client = OpenAIClient()
|
||||||
response = client.get_response(test_prompt)
|
test_response = client.get_response(test_prompt)
|
||||||
|
|
||||||
# Print messages and response
|
# Create response adapter and add response to prompt
|
||||||
|
response_adapter = OpenAIResponseAdapter(test_response)
|
||||||
|
response_message = response_adapter.to_message()
|
||||||
|
prompt_builder.add_message(response_message)
|
||||||
|
|
||||||
|
# Print messages
|
||||||
print(test_prompt.to_list)
|
print(test_prompt.to_list)
|
||||||
print(response)
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user