Get LLM output as Python object with Langchain and Pydantic | Hands-on tutorial

Опубликовано: 21 Декабрь 2023
на канале: Data Science Garage
5,206
109

The #Pydantic output parser is a tool that allows users to define a JSON schema to query LLMs for outputs that adhere to that schema. This is pivotal for applications that require structured data, as it ensures outputs conform to predefined formats. The parser leverages Pydantic’s BaseModel for data validation and type checking.

With this tutorial you will learn to: set up the data model in a way that Langchain’s output parser can be used to generate structured data.

For example, you can grab from #LLM not a plain text (as an answer), but re-usable Python objects, such as Python list, dictionary, Pandas dataframe and more.

This functionality allows you to create super powerful LLM applications where any kind of data transformation, parse or passing to ML models (such as example) are required.

To utilize this parser, one must define the data structure using Pydantic’s BaseModel. You will learn that in the tutorial.

Useful links and references:
Pydantic (JSON) Parser: https://python.langchain.com/docs/mod...
PydanticOutputParser: https://api.python.langchain.com/en/l...
LLMs supported by LangChain: https://python.langchain.com/docs/mod...

In this tutorial I used OpenAI's ChatGPT API to support LLM. Feel free to use any supported LLM using LangChain (now testing on IBM WatsonX).

LInk to Github repository with the original code used in this tutorial: https://github.com/vb100/langchain_py...

The content of the tutorial:
0:00 - Main idea using Pydantic with Langchain
1:09 - Implementation scheme for hands-on
3:29 - Hands-on part (coding)
15:06 - BONUS: Github repo

#langchain

Happy learning!


Смотрите видео Get LLM output as Python object with Langchain and Pydantic | Hands-on tutorial онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Data Science Garage 21 Декабрь 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 5,206 раз и оно понравилось 109 людям.