Package: transformgpt
TransformGPTTransformGPT is a python library for interpreting unstructured (or structured) data into Python objects using ChatGPT. Given a Python class hierarchy, it can take arbitrary data and structure in into the class hierarchy as a series of objects. This is useful for convertung natural language into structured data, or for converting one data type into another without specifying the mapping schema.Installation:pip install transformgptOptional:Set an environment variable calledOpenAIAPI-Tokento your OpenAI API token.Command line:usage: transformgpt [-h]
                    [-k KEY]
                    [-m MODEL]
                    [-t TEMPERATURE]
                    descriptionKEY: OpenAI API Token if it's not set as an environment variable.MODEL: OpenAI Model, defaults to 'gpt-3.5-turbo'TEMPERATURE: Temperature for the ChatCompletion, defaults to 0, increase towards 1 to make the answers more creativeDescription: A description of how you want the data to be transformed.Takes input from STDIN and returns on STDOUT the transformed data.Example:> echo "Hello World.\nHow are you today?\nI am fine.\nIf you don't respond I will blackmail you." | transformgpt "An object with the fields original_message, intent, and response, where intent is one of greeting, inquiry, response, threat, informative."Yields:-original_message:"HelloWorld."intent:greetingresponse:"Hithere!"-original_message:"Howareyoutoday?"intent:inquiryresponse:"I'mdoingwell,thankyou.Howaboutyou?"-original_message:"Iamfine."intent:responseresponse:"Gladtohearthat!"-original_message:"Ifyoudon'trespondIwillblackmailyou."intent:threatresponse:"I'msorry,Ididn'tmeantoignoreyou.Istheresomethingyouneedhelpwith?"Python usage:importtransformgptimportopenaiimportosopenai.api_key="YOUR OPENAI TOKEN"transformer=transformgpt.TransformGPT(openai.ChatCompletion)classMessage:def__init__(self,message:str,data:dict[str,str]):self.message=messageself.data=dataincoming_message="The message is tell Joey Tracy is cheating on him with maid. The data to include is Orange is the new black, and the only way to get the job done is to do it yourself."print(transformer.transform_string(incoming_message,Message))Yields:Message(message="Tell Joey Tracy is cheating on him with the maid.", data={"Orange": "The new black.", "The only way to get the job done": "Do it yourself."})Datalasses/Nested StructuresIt handles @dataclasses, and nested class hierarcharies as well:from__future__importannotationsfromdataclassesimportdataclassimportdataclassesfromtypingimportList,Optionalimportopenaiimportosimporttransformgptimportyamlopenai.api_key="YOUR OPENAI TOKEN"transformer=transformgpt.TransformGPT(openai.ChatCompletion)@dataclassclassMessageClassification:original_message:strmessage_part:strintent:Optional[str]=Nonecategories:List[str]=dataclasses.field(default_factory=list)parameters:List[str]=dataclasses.field(default_factory=list)reply:Optional[str]=Nonejustifications_for_reply:List[Justification]=dataclasses.field(default_factory=list)follow_up_items:List[MessageClassification]=dataclasses.field(default_factory=list)@dataclassclassJustification:subject:Optional[str]=Noneobject:Optional[str]=Noneintent:Optional[str]=Noneaction:Optional[str]=Nonedescription:Optional[str]=Noneincoming_message="The message is tell Joey Tracy is cheating on him with maid. The data to include is Orange is the new black, and the only way to get the job done is to do it yourself."print(yaml.dump(transformer.transform_string(incoming_message,MessageClassification)))Yields:- !!python/object:__main__.MessageClassification
  categories:
  - Relationships
  - Infidelity
  follow_up_items:
  - !!python/object:__main__.MessageClassification
    categories:
    - Entertainment
    - Motivation
    follow_up_items: []
    intent: null
    justifications_for_reply:
    - !!python/object:__main__.Justification
      action: null
      description: Orange is the new black is a popular TV show.
      intent: null
      object: null
      subject: null
    - !!python/object:__main__.Justification
      action: null
      description: Doing it yourself is the best way to ensure it gets done right.
      intent: null
      object: null
      subject: null
    message_part: The data to include is Orange is the new black, and the only way
      to get the job done is to do it yourself.
    original_message: The data to include is Orange is the new black, and the only
      way to get the job done is to do it yourself.
    parameters: []
    reply: null
  intent: null
  justifications_for_reply:
  - !!python/object:__main__.Justification
    action: null
    description: null
    intent: Cheating
    object: Joey
    subject: Tracy
  - !!python/object:__main__.Justification
    action: Involved in cheating
    description: null
    intent: null
    object: null
    subject: Maid
  message_part: Tell Joey Tracy is cheating on him with maid.
  original_message: The message is tell Joey Tracy is cheating on him with maid.
  parameters: []
  reply: nullIt also supports transforming one object into another:result=transformer.transform_object(myListOfObjects,MyDataTypeToTransformInto)#Returns a list[MyDataTypeToTransformInto]