Runnable-Country
Sat 17 May 2025
!python --version
Python 3.12.4
# https://python.langchain.com/docs/how_to/streaming/
from constants import OPENAI_API_KEY
!pip show langchain-openai | grep "Version:"
Version: 0.2.9
import os
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o-mini")
country_info = {
"1001" : "india",
"1002" : "canada"
}
country_more_details = {
"india" : {
"capital" : "New Delhi",
"youth_index" : 4
},
"india" : {
"capital" : "Ottawa",
"youth_index" : 8
}
}
from langchain_core.runnables import chain
@chain
async def get_country_info(word: str):
if word in country_info.keys():
return country_info[word]
return "unknown"
@chain
async def fill_info(word: str):
c_info = await get_country_info.ainvoke(word)
if c_info == "unknown":
return {}
if c_info in country_more_details:
return country_more_details[c_info]
return {}
await fill_info.ainvoke("1001")
{'capital': 'Ottawa', 'youth_index': 8}
Score: 15
Category: langchain