run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
33.11k stars 4.61k forks source link

[Bug]: Documentation for guidance_pydantic is out of date #13962

Open zeloras opened 3 weeks ago

zeloras commented 3 weeks ago

Bug Description

Good evening! it seems that the https://docs.llamaindex.ai/en/stable/examples/output_parsing/guidance_pydantic_program/ documentation is out of time, could you take a look? those example's giving exceptions

llama-index==0.10.43 llama-index-program-guidance==0.1.2

image

Version

0.10.43

Steps to Reproduce

Please check the colab on the 3rd step(imports) https://docs.llamaindex.ai/en/stable/examples/output_parsing/guidance_pydantic_program/

Relevant Logs/Tracbacks

ImportError                               Traceback (most recent call last)

<ipython-input-3-fc0a30d8938f> in <cell line: 1>()
----> 1 from llama_index.question_gen.guidance import GuidanceQuestionGenerator
      2 from guidance.llms import OpenAI as GuidanceOpenAI

3 frames

/usr/local/lib/python3.10/dist-packages/llama_index/program/guidance/base.py in <module>
     11 from guidance import assistant, gen, user
     12 from guidance.models import Model as GuidanceLLM
---> 13 from guidance.models import OpenAIChat
     14 
     15 

ImportError: cannot import name 'OpenAIChat' from 'guidance.models' (/usr/local/lib/python3.10/dist-packages/guidance/models/__init__.py)
dosubot[bot] commented 3 weeks ago

Good evening, @zeloras! I'm here to assist you with any bugs, questions, or contributions you have. Let's tackle this issue together!

To resolve the import errors in the "Guidance Pydantic Program" documentation for the LlamaIndex package, ensure that the following imports are correctly included at the beginning of your script:

from functools import partial
from typing import Any, Optional, Type, cast

from llama_index.core.bridge.pydantic import BaseModel
from llama_index.core.program.llm_prompt_program import BaseLLMFunctionProgram
from llama_index.core.prompts.base import PromptTemplate
from llama_index.program.guidance.utils import (
    parse_pydantic_from_guidance_program,
)

from guidance import assistant, gen, user
from guidance.models import Model as GuidanceLLM
from guidance.models import OpenAI

Additionally, make sure that you have the GuidancePydanticProgram class correctly imported from the llama_index.program.guidance.base module:

from llama_index.program.guidance.base import GuidancePydanticProgram

These imports are necessary for the GuidancePydanticProgram class to function correctly. Make sure that all the modules and classes being imported are available in your environment. If any of these modules are missing, you may need to install them or check their paths.

Also, ensure that you are using the correct version of Pydantic. The llama_index.core.bridge.pydantic module attempts to import pydantic.v1 first, and falls back to pydantic if the former is not available. Make sure you have pydantic.v1 installed or adjust the imports accordingly:

try:
    import pydantic.v1 as pydantic
    from pydantic.v1 import (
        BaseConfig,
        BaseModel,
        Field,
        PrivateAttr,
        StrictFloat,
        StrictInt,
        StrictStr,
        create_model,
        root_validator,
        validator,
    )
    from pydantic.v1.error_wrappers import ValidationError
    from pydantic.v1.fields import FieldInfo
    from pydantic.v1.generics import GenericModel
except ImportError:
    import pydantic  # type: ignore
    from pydantic import (
        BaseConfig,
        BaseModel,
        Field,
        PrivateAttr,
        StrictFloat,
        StrictInt,
        StrictStr,
        create_model,
        root_validator,
        validator,
    )
    from pydantic.error_wrappers import ValidationError
    from pydantic.fields import FieldInfo
    from pydantic.generics import GenericModel

This ensures compatibility with the Guidance Pydantic Program and resolves potential import errors [1][2][3][4].

To continue talking to Dosu, mention @dosu.