langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.34k stars 14.76k forks source link

Invoke method with JsonOutputParser returns JSON properties instead of response #22189

Closed th-cds closed 1 week ago

th-cds commented 3 months ago

Checked other resources

Example Code


from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.output_parsers import JsonOutputParser
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from pprint import pprint
import os
from dotenv import load_dotenv

load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
class UserStory_add(BaseModel):
        PrimaryActions: str = Field(description="The main phrasal verb or verb. It can be VB, VB + RP or VB + IN.")
        PrimaryEntities: str = Field(description="The direct objects, nouns with their immediate modifiers, of the primary actions in the user story. They can be NN, NN+noun modifiers, NN+JJ, or NN + CD.")
        SecondaryActions: list = Field(description="The remaining verbs or phrasal verbs in goal and benefit. They can be VB, VB + RP or VB + IN.")
        SecondaryEntities: list = Field(description="The remaining entities in goal and benefit, nouns with their immediate modifiers, that are not primary entities. They can be NN + noun modifiers , NN+ JJ, or NN + CD.")

def create_prompt_add_goal():
    template = """"
    You are an NLP specialist.
    Given a sentence, your task is to extract specific linguistic elements using NLTK's POS tagging.
    1. Identify the primary action in the sentence. This action is the main verb or phrasal verb and should not have more than two POS tags.
    2. Determine the primary entity associated with the primary action. This entity is the direct object of the primary action and should be a noun with its immediate modifiers.
    3. Extract any secondary actions present in the sentence. Secondary actions are verbs or phrasal verbs that are not the primary action.
    4. Identify secondary entities, which are nouns with their immediate modifiers, excluding the primary entity. 
    Conjunctions should not be considered part of primary or secondary entities, they only separate two entities.

    Please ensure that the extraction is performed accurately according to the provided guidelines.

    Extract this information from the sentence:
    {sentence}.
    Format instructions: {format_instructions}
    """
    return PromptTemplate.from_template(template = template)

if __name__ == "__main__":
    model = ChatOpenAI(model='gpt-3.5-turbo-0125', temperature=0)

    sentence="so that I can get approvals from leadership."

    parser = JsonOutputParser(pydantic_object=UserStory_add)
    format_instructions_add = parser.get_format_instructions()

    prompt = create_prompt_add_goal()

    chain = prompt | model | parser

    result = chain.invoke({"sentence":sentence, "format_instructions":format_instructions_add})

    pprint(result)
´´´

### Error Message and Stack Trace (if applicable)

{'properties': {'PrimaryActions': {'description': 'The main phrasal verb or '
                                                  'verb. It can be VB, VB + RP '
                                                  'or VB + IN.',
                                   'title': 'Primaryactions',
                                   'type': 'string'},
                'PrimaryEntities': {'description': 'The direct objects, nouns '
                                                   'with their immediate '
                                                   'modifiers, of the primary '
                                                   'actions in the user story. '
                                                   'They can be NN, NN+noun '
                                                   'modifiers, NN+JJ, or NN + '
                                                   'CD.',
                                    'title': 'Primaryentities',
                                    'type': 'string'},
                'SecondaryActions': {'description': 'The remaining verbs or '
                                                    'phrasal verbs in goal and '
                                                    'benefit. They can be VB, '
                                                    'VB + RP or VB + IN.',
                                     'items': {},
                                     'title': 'Secondaryactions',
                                     'type': 'array'},
                'SecondaryEntities': {'description': 'The remaining entities '
                                                     'in goal and benefit, '
                                                     'nouns with their '
                                                     'immediate modifiers, '
                                                     'that are not primary '
                                                     'entities. They can be NN '
                                                     '+ noun modifiers , NN+ '
                                                     'JJ, or NN + CD.',
                                      'items': {},
                                      'title': 'Secondaryentities',
                                      'type': 'array'}},
 'required': ['PrimaryActions',
              'PrimaryEntities',
              'SecondaryActions',
              'SecondaryEntities']}

### Description

I am using JsonOutputParser to get a structured answer from the llm, when I run multiple tests, I encounter this reply that instead of giving me the reply from the llm, gives me the properties of the json output format.

### System Info

langchain==0.2.0
langchain-community==0.2.0
langchain-core==0.2.0
langchain-openai==0.1.7
langchain-text-splitters==0.2.0
keenborder786 commented 3 months ago

Can you please update the packages, because for me everything is working fine.

dottedr commented 3 months ago

I have the same issue. I have the newest package versions too.



langchain-core==0.2.1

langchain-openai==0.1.7

langchain-text-splitters==0.2.0 ; python_version < '4.0' and python_full_version >= '3.8.1'

langsmith==0.1.63 ; python_version < '4.0' and python_full_version >= '3.8.1'
th-cds commented 3 months ago

Can you please update the packages, because for me everything is working fine.

It happens randomly, when I execute multiple times I can observe this response.