Skip to content

Commit

Permalink
Add remapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-XT committed May 3, 2024
1 parent 9139550 commit b06c5f5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 32 additions & 2 deletions convertanything/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic import BaseModel
from typing import Type, get_args, get_origin, Union
from typing import Type, get_args, get_origin, Union, List
import json
import openai
from enum import Enum
Expand All @@ -8,8 +8,8 @@
def convertanything(
input_string: str,
model: Type[BaseModel],
api_key=None,
server="https://api.openai.com",
api_key=None,
llm="gpt-3.5-turbo-16k",
):
input_string = str(input_string)
Expand Down Expand Up @@ -58,3 +58,33 @@ def convertanything(
print(response)
print("Failed to convert the response to the model, trying again.")
return convertanything(input_string=input_string, model=model)


def remap_fields(converted_data: dict, data: List[dict]) -> List[dict]:
mapped_list = []
for info in data:
new_data = {}
for key, value in converted_data.items():
item = [k for k, v in data[0].items() if v == value]
if item:
new_data[key] = info[item[0]]
mapped_list.append(new_data)
return mapped_list


def convert_list_of_dicts(
data: List[dict],
model: Type[BaseModel],
server="https://api.openai.com",
api_key=None,
llm="gpt-3.5-turbo-16k",
):
converted_data = convertanything(
input_string=json.dumps(data[0]),
model=model,
server=server,
api_key=api_key,
llm=llm,
)
mapped_list = remap_fields(converted_data=converted_data.model_dump(), data=data)
return mapped_list
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="convertanything",
version="0.0.10",
version="0.0.11",
description="convertanything is a Python package that allows you to convert any text into a structured format according to the schema provided.",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit b06c5f5

Please sign in to comment.