mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-10 11:55:11 +03:00
+26









Erick Friis
Harrison Chase
jacoblee93
Leonid Ganeline
Leonid Kuligin
Averi Kitsch
Nuno Campos
Nuno Campos
Bagatur
Eugene Yurtsev
Martín Gotelli Ferenaz
Fayfox
Eugene Yurtsev
Dawson Bauer
Ravindu Somawansa
Dhruv Chawla
ccurme
Bagatur
WeichenXu
Benito Geordie
kartikTAI
Kartik Sarangmath
Sevin F. Varoglu
MacanPN
Prashanth Rao
Hyeongchan Kim
sdan
Guangdong Liu
Rahul Triptahi
Rahul Tripathi
pjb157
Eun Hye Kim
kaijietti
Pengcheng Liu
Tomer Cagan
Christophe Bornet
21d14549a9
current python.langchain.com is building from branch `v0.1`. Iterate on v0.2 docs here. --------- Signed-off-by: Weichen Xu <weichen.xu@databricks.com> Signed-off-by: Rahul Tripathi <rauhl.psit.ec@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: jacoblee93 <jacoblee93@gmail.com> Co-authored-by: Leonid Ganeline <leo.gan.57@gmail.com> Co-authored-by: Leonid Kuligin <lkuligin@yandex.ru> Co-authored-by: Averi Kitsch <akitsch@google.com> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Nuno Campos <nuno@boringbits.io> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Martín Gotelli Ferenaz <martingotelliferenaz@gmail.com> Co-authored-by: Fayfox <admin@fayfox.com> Co-authored-by: Eugene Yurtsev <eugene@langchain.dev> Co-authored-by: Dawson Bauer <105886620+djbauer2@users.noreply.github.com> Co-authored-by: Ravindu Somawansa <ravindu.somawansa@gmail.com> Co-authored-by: Dhruv Chawla <43818888+Dominastorm@users.noreply.github.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: WeichenXu <weichen.xu@databricks.com> Co-authored-by: Benito Geordie <89472452+benitoThree@users.noreply.github.com> Co-authored-by: kartikTAI <129414343+kartikTAI@users.noreply.github.com> Co-authored-by: Kartik Sarangmath <kartik@thirdai.com> Co-authored-by: Sevin F. Varoglu <sfvaroglu@octoml.ai> Co-authored-by: MacanPN <martin.triska@gmail.com> Co-authored-by: Prashanth Rao <35005448+prrao87@users.noreply.github.com> Co-authored-by: Hyeongchan Kim <kozistr@gmail.com> Co-authored-by: sdan <git@sdan.io> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: Rahul Triptahi <rahul.psit.ec@gmail.com> Co-authored-by: Rahul Tripathi <rauhl.psit.ec@gmail.com> Co-authored-by: pjb157 <84070455+pjb157@users.noreply.github.com> Co-authored-by: Eun Hye Kim <ehkim1440@gmail.com> Co-authored-by: kaijietti <43436010+kaijietti@users.noreply.github.com> Co-authored-by: Pengcheng Liu <pcliu.fd@gmail.com> Co-authored-by: Tomer Cagan <tomer@tomercagan.com> Co-authored-by: Christophe Bornet <cbornet@hotmail.com>
9.3 KiB
9.3 KiB
In [1]:
from typing import List
from langchain.output_parsers import PydanticOutputParser
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAIIn [2]:
class Actor(BaseModel):
name: str = Field(description="name of an actor")
film_names: List[str] = Field(description="list of names of films they starred in")
actor_query = "Generate the filmography for a random actor."
parser = PydanticOutputParser(pydantic_object=Actor)In [3]:
misformatted = "{'name': 'Tom Hanks', 'film_names': ['Forrest Gump']}"In [4]:
parser.parse(misformatted)[0;31m---------------------------------------------------------------------------[0m
[0;31mJSONDecodeError[0m Traceback (most recent call last)
File [0;32m~/workplace/langchain/libs/langchain/langchain/output_parsers/pydantic.py:29[0m, in [0;36mPydanticOutputParser.parse[0;34m(self, text)[0m
[1;32m 28[0m json_str [38;5;241m=[39m match[38;5;241m.[39mgroup()
[0;32m---> 29[0m json_object [38;5;241m=[39m [43mjson[49m[38;5;241;43m.[39;49m[43mloads[49m[43m([49m[43mjson_str[49m[43m,[49m[43m [49m[43mstrict[49m[38;5;241;43m=[39;49m[38;5;28;43;01mFalse[39;49;00m[43m)[49m
[1;32m 30[0m [38;5;28;01mreturn[39;00m [38;5;28mself[39m[38;5;241m.[39mpydantic_object[38;5;241m.[39mparse_obj(json_object)
File [0;32m~/.pyenv/versions/3.10.1/lib/python3.10/json/__init__.py:359[0m, in [0;36mloads[0;34m(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)[0m
[1;32m 358[0m kw[[38;5;124m'[39m[38;5;124mparse_constant[39m[38;5;124m'[39m] [38;5;241m=[39m parse_constant
[0;32m--> 359[0m [38;5;28;01mreturn[39;00m [38;5;28;43mcls[39;49m[43m([49m[38;5;241;43m*[39;49m[38;5;241;43m*[39;49m[43mkw[49m[43m)[49m[38;5;241;43m.[39;49m[43mdecode[49m[43m([49m[43ms[49m[43m)[49m
File [0;32m~/.pyenv/versions/3.10.1/lib/python3.10/json/decoder.py:337[0m, in [0;36mJSONDecoder.decode[0;34m(self, s, _w)[0m
[1;32m 333[0m [38;5;250m[39m[38;5;124;03m"""Return the Python representation of ``s`` (a ``str`` instance[39;00m
[1;32m 334[0m [38;5;124;03mcontaining a JSON document).[39;00m
[1;32m 335[0m
[1;32m 336[0m [38;5;124;03m"""[39;00m
[0;32m--> 337[0m obj, end [38;5;241m=[39m [38;5;28;43mself[39;49m[38;5;241;43m.[39;49m[43mraw_decode[49m[43m([49m[43ms[49m[43m,[49m[43m [49m[43midx[49m[38;5;241;43m=[39;49m[43m_w[49m[43m([49m[43ms[49m[43m,[49m[43m [49m[38;5;241;43m0[39;49m[43m)[49m[38;5;241;43m.[39;49m[43mend[49m[43m([49m[43m)[49m[43m)[49m
[1;32m 338[0m end [38;5;241m=[39m _w(s, end)[38;5;241m.[39mend()
File [0;32m~/.pyenv/versions/3.10.1/lib/python3.10/json/decoder.py:353[0m, in [0;36mJSONDecoder.raw_decode[0;34m(self, s, idx)[0m
[1;32m 352[0m [38;5;28;01mtry[39;00m:
[0;32m--> 353[0m obj, end [38;5;241m=[39m [38;5;28;43mself[39;49m[38;5;241;43m.[39;49m[43mscan_once[49m[43m([49m[43ms[49m[43m,[49m[43m [49m[43midx[49m[43m)[49m
[1;32m 354[0m [38;5;28;01mexcept[39;00m [38;5;167;01mStopIteration[39;00m [38;5;28;01mas[39;00m err:
[0;31mJSONDecodeError[0m: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
During handling of the above exception, another exception occurred:
[0;31mOutputParserException[0m Traceback (most recent call last)
Cell [0;32mIn[4], line 1[0m
[0;32m----> 1[0m [43mparser[49m[38;5;241;43m.[39;49m[43mparse[49m[43m([49m[43mmisformatted[49m[43m)[49m
File [0;32m~/workplace/langchain/libs/langchain/langchain/output_parsers/pydantic.py:35[0m, in [0;36mPydanticOutputParser.parse[0;34m(self, text)[0m
[1;32m 33[0m name [38;5;241m=[39m [38;5;28mself[39m[38;5;241m.[39mpydantic_object[38;5;241m.[39m[38;5;18m__name__[39m
[1;32m 34[0m msg [38;5;241m=[39m [38;5;124mf[39m[38;5;124m"[39m[38;5;124mFailed to parse [39m[38;5;132;01m{[39;00mname[38;5;132;01m}[39;00m[38;5;124m from completion [39m[38;5;132;01m{[39;00mtext[38;5;132;01m}[39;00m[38;5;124m. Got: [39m[38;5;132;01m{[39;00me[38;5;132;01m}[39;00m[38;5;124m"[39m
[0;32m---> 35[0m [38;5;28;01mraise[39;00m OutputParserException(msg, llm_output[38;5;241m=[39mtext)
[0;31mOutputParserException[0m: Failed to parse Actor from completion {'name': 'Tom Hanks', 'film_names': ['Forrest Gump']}. Got: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)In [5]:
from langchain.output_parsers import OutputFixingParser
new_parser = OutputFixingParser.from_llm(parser=parser, llm=ChatOpenAI())In [6]:
new_parser.parse(misformatted)Out [6]:
Actor(name='Tom Hanks', film_names=['Forrest Gump'])
In [ ]: