安装依赖
pip install --upgrade --quiet langchain-core langchain-community langchain-openai • 1
我在环境变量中配置了,所以后续在代码中可以省略这个参数。
export OPENAI_API_KEY="sk-UkyBxxxx" export OPENAI_API_BASE="https://wzk....." • 1 • 2
编写代码
from langchain_core.output_parsers import JsonOutputParser from langchain_openai.chat_models import ChatOpenAI async def main(): model = ChatOpenAI( model="gpt-3.5-turbo", ) chain = ( model | JsonOutputParser() ) # Due to a bug in older versions of Langchain, JsonOutputParser did not stream results from some models async for text in chain.astream( 'output a list of the countries france, spain and japan and their populations in JSON format. Use a dict with an outer key of "countries" which contains a list of countries. Each country should have the key `name` and `population`' ): print(text, flush=True) if __name__ == '__main__': import asyncio asyncio.run(main())
执行代码
➜ python3 test04.py {} {'countries': []} {'countries': [{}]} {'countries': [{'name': ''}]} {'countries': [{'name': 'France'}]} {'countries': [{'name': 'France', 'population': 670}]} {'countries': [{'name': 'France', 'population': 670760}]} {'countries': [{'name': 'France', 'population': 67076000}]} {'countries': [{'name': 'France', 'population': 67076000}, {}]} {'countries': [{'name': 'France', 'population': 67076000}, {'name': ''}]} {'countries': [{'name': 'France', 'population': 67076000}, {'name': 'Spain'}]} {'countries': [{'name': 'France', 'population': 67076000}, {'name': 'Spain', 'population': 467}]} {'countries': [{'name': 'France', 'population': 67076000}, {'name': 'Spain', 'population': 467330}]} {'countries': [{'name': 'France', 'population': 67076000}, {'name': 'Spain', 'population': 46733038}]} {'countries': [{'name': 'France', 'population': 67076000}, {'name': 'Spain', 'population': 46733038}, {}]} {'countries': [{'name': 'France', 'population': 67076000}, {'name': 'Spain', 'population': 46733038}, {'name': ''}]} {'countries': [{'name': 'France', 'population': 67076000}, {'name': 'Spain', 'population': 46733038}, {'name': 'Japan'}]} {'countries': [{'name': 'France', 'population': 67076000}, {'name': 'Spain', 'population': 46733038}, {'name': 'Japan', 'population': 126}]} {'countries': [{'name': 'France', 'population': 67076000}, {'name': 'Spain', 'population': 46733038}, {'name': 'Japan', 'population': 126476}]} {'countries': [{'name': 'France', 'population': 67076000}, {'name': 'Spain', 'population': 46733038}, {'name': 'Japan', 'population': 126476458}]}
提取JSON
定义一个函数
def _extract_country_names(inputs): """A function that does not operates on input streams and breaks streaming.""" if not isinstance(inputs, dict): return "" if "countries" not in inputs: return "" countries = inputs["countries"] if not isinstance(countries, list): return "" country_names = [ country.get("name") for country in countries if isinstance(country, dict) ] return country_names
通过 Chain
将函数链接起来
# 上文写的是:model | JsonOutputParser(),这里修改为: chain = model | JsonOutputParser() | _extract_country_names
再次执行
➜ python3 test04.py [None, '', 'France', 'France', 'France', 'France', 'France', None, 'France', '', 'France