eyurtsev / kor

LLM(😽)
https://eyurtsev.github.io/kor/
MIT License
1.59k stars 88 forks source link

[BUG] TypeError: initial_value must be str or None, not dict #196

Closed 5102a closed 1 year ago

5102a commented 1 year ago

name: Bug Report labels: bug assignees: ''


Describe the bug

The code in the "Working With Objects" document example appears to not be running correctly.

kor is using version 0.13.0.

To Reproduce

There appear to be some errors when using this demo.

https://eyurtsev.github.io/kor/objects.html#working-with-objects

code:

from kor.extraction import create_extraction_chain
from kor.nodes import Object, Text, Number
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
from variables import OPENAI_API_KEY
llm = ChatOpenAI(
    model_name="gpt-3.5-turbo",
    openai_api_key=OPENAI_API_KEY,
    temperature=0,
    max_tokens=2000,
    frequency_penalty=0,
    presence_penalty=0,
    top_p=1.0,
)
schema = Object(
    id="personal_info",
    description="Personal information about a given person.",
    attributes=[
        Text(
            id="first_name",
            description="The first name of the person",
            examples=[("John Smith went to the store", "John")],
        ),
        Text(
            id="last_name",
            description="The last name of the person",
            examples=[("John Smith went to the store", "Smith")],
        ),
        Number(
            id="age",
            description="The age of the person in years.",
            examples=[("23 years old", "23"), ("I turned three on sunday", "3")],
        ),
    ],
    examples=[
        (
            "John Smith was 23 years old. He was very tall. He knew Jane Doe. She was 5 years old.",
            [
                {"first_name": "John", "last_name": "Smith", "age": 23},
                {"first_name": "Jane", "last_name": "Doe", "age": 5},
            ],
        )
    ],
    many=True,
)
chain = create_extraction_chain(llm, schema)
print(
    chain.predict_and_parse(
        text=(
            "My name is Bob Alice and my phone number is (123)-444-9999. I found my true love one"
            " on a blue sunday. Her number was (333)1232832. Her name was Moana Sunrise and she was 10 years old."
        )
    )["data"]
)

error stack:

TypeError                                 Traceback (most recent call last)
Cell In[4], line 3
      1 chain = create_extraction_chain(llm, schema)
      2 print(
----> 3     chain.predict_and_parse(
      4         text=(
      5             "My name is Bob Alice and my phone number is (123)-444-9999. I found my true love one"
      6             " on a blue sunday. Her number was (333)1232832. Her name was Moana Sunrise and she was 10 years old."
      7         )
      8     )["data"]
      9 )

File [c:\Users\xxx\anaconda3\envs\demo\lib\site-packages\langchain\chains\llm.py:281](file:///C:/Users/xxx/anaconda3/envs/demo/lib/site-packages/langchain/chains/llm.py:281), in LLMChain.predict_and_parse(self, callbacks, **kwargs)
    279 result = self.predict(callbacks=callbacks, **kwargs)
    280 if self.prompt.output_parser is not None:
--> 281     return self.prompt.output_parser.parse(result)
    282 else:
    283     return result

File [c:\Users\xxx\anaconda3\envs\demo\lib\site-packages\kor\extraction\parser.py:38](file:///C:/Users/xxx/anaconda3/envs/demo/lib/site-packages/kor/extraction/parser.py:38), in KorParser.parse(self, text)
     36 """Parse the text."""
     37 try:
---> 38     data = self.encoder.decode(text)
     39 except ParseError as e:
...
   (...)
    102                 skipinitialspace=True,
    103             )

TypeError: initial_value must be str or None, not dict

Expected behavior

{'personal_info': [{'first_name': 'Bob', 'last_name': 'Alice', 'age': ''}, {'first_name': 'Moana', 'last_name': 'Sunrise', 'age': '10'}]}

Screenshots

image

Additional context

eyurtsev commented 1 year ago

Try running this with .run instead of predict_and_parse. Let me know if it works.

Last code change was likely a breaking change so should've had a major version bump.

5102a commented 1 year ago

Try running this with .run instead of predict_and_parse. Let me know if it works.

Last code change was likely a breaking change so should've had a major version bump.

Using 'run' can produce the expected output, so i can currently use it as a replacement for 'predict_and_parse'. Thanks

eyurtsev commented 1 year ago

I updated all the remaining docs -- thanks for letting me know! Marking issue as resolved.