from langchain_community.tools.wikidata.tool import WikidataAPIWrapper, WikidataQueryRun
from langchain_openai import ChatOpenAI
from langchain import hub
from langchain.agents import create_openai_functions_agent
from langchain.agents import AgentExecutor
from langchain.tools import BaseTool, StructuredTool, tool
from langchain.agents import load_tools
from langchain.callbacks import get_openai_callback
llm = ChatOpenAI(model='gpt-4-1106-preview')
wikidata = WikidataQueryRun(api_wrapper=WikidataAPIWrapper())
prompt = hub.pull("hwchase17/openai-functions-agent")
tools = [wikidata]
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "When was the paper 'Respect limits of embryo patents' published?"})
> Entering new AgentExecutor chain... Invoking: `Wikidata` with `Respect limits of embryo patents` Label: Respect limits of embryo patents Description: scientific article published on 01 June 2011 instance of: scholarly article publication date: 2011-06-01The paper 'Respect limits of embryo patents' was published on June 1, 2011. > Finished chain.
{'input': "When was the paper 'Respect limits of embryo patents' published?", 'output': "The paper 'Respect limits of embryo patents' was published on June 1, 2011."}
agent_executor.invoke({"input": "Laura Loomer is a member of what Political Party?"})
> Entering new AgentExecutor chain... Invoking: `Wikidata` with `Laura Loomer` Label: Laura Loomer Description: American far-right activist Aliases: Laura Elizabeth Loomer instance of: human country of citizenship: United States of America occupation: journalist, political activist, politician sex or gender: female date of birth: 1993-05-21 place of birth: Tucson educated at: Mount Holyoke College, Barry University employer: Project Veritas, InfoWars, Rebel News member of political party: Republican Party political ideology: Islamophobia, white nationalism political alignment: right-wing, far-right, alt-lite, alt-right religion or worldview: Judaism Label: Live: The Loom's Desire Description: 2002 Laura Nyro live album Aliases: Live! The Loom's Desire, The Loom's Desire, Live/The Loom's Desire, Live / The Loom's Desire, The Loom’s Desire, Live/The Loom’s Desire, Live / The Loom’s Desire, Live: The Loom’s Desire, Live! The Loom’s Desire instance of: album publication date: 2002-05-07 performer: Laura Nyro form of creative work: double album, live albumLaura Loomer is a member of the Republican Party. > Finished chain.
{'input': 'Laura Loomer is a member of what Political Party?', 'output': 'Laura Loomer is a member of the Republican Party.'}
agent_executor.invoke({"input": "Where is HuggingFace headquartered?"})
> Entering new AgentExecutor chain... Invoking: `Wikidata` with `Hugging Face` Label: Hugging Face Description: American company Aliases: Hugging Face, Inc. instance of: business, virtual community country: United States of America inception: 2016 field of work: machine learning headquarters location: Brooklyn, Manhattan Label: Hugging Face SAS instance of: enterprise country: France industry: computer programmingHugging Face is headquartered in Brooklyn, Manhattan, United States of America. There is also a related entity, Hugging Face SAS, which is an enterprise located in France and works in the computer programming industry. > Finished chain.
{'input': 'Where is HuggingFace headquartered?', 'output': 'Hugging Face is headquartered in Brooklyn, Manhattan, United States of America. There is also a related entity, Hugging Face SAS, which is an enterprise located in France and works in the computer programming industry.'}
agent_executor.invoke({"input": "What year was the director of 'Primer' born using only information from wikidata?"})
> Entering new AgentExecutor chain... Invoking: `Wikidata` with `Primer (film)` Label: Primer Description: 2004 film directed by Shane Carruth instance of: film country of origin: United States of America publication date: 2004-10-08 genre: science fiction film, drama film, arthouse science fiction film cast member: David Sullivan, Shane Carruth director: Shane Carruth Label: Mechanical and Optical Properties of Thermochromic Reversible Waterborne Primer Film on Tilia europaea with 1,2-Benzo-6-diethylaminofluorane Based Microcapsules Description: scientific article published on 10 September 2020 instance of: scholarly article publication date: 2020-09-10 Invoking: `Wikidata` with `Shane Carruth` Label: Shane Carruth Description: film producer, actor, screenwriter instance of: human country of citizenship: United States of America occupation: screenwriter, film producer, film director, actor, musician, film editor, television actor, film actor sex or gender: male date of birth: 1972 place of birth: Myrtle Beach educated at: Stephen F. Austin State University, J. J. Pearce High School Label: Primer Description: 2004 film directed by Shane Carruth instance of: film country of origin: United States of America publication date: 2004-10-08 genre: science fiction film, drama film, arthouse science fiction film cast member: David Sullivan, Shane Carruth director: Shane CarruthThe director of the film 'Primer,' Shane Carruth, was born in the year 1972. > Finished chain.
{'input': "What year was the director of 'Primer' born using only information from wikidata?", 'output': "The director of the film 'Primer,' Shane Carruth, was born in the year 1972."}
agent_executor.invoke({"input": "When did the House episode 'Insensitive' air?"})
> Entering new AgentExecutor chain... Invoking: `Wikidata` with `Insensitive (House)` Label: Insensitive Description: episode of House (S3 E14) instance of: television series episode publication date: 2007-02-13 part of the series: House director: Deran Sarafian Label: Clinical manifestations of congenital insensitivity of the hand and classification of syndromes Description: scientific article published on 01 November 1984 instance of: scholarly article publication date: 1984-11-01The "House" episode titled "Insensitive" aired on February 13, 2007. > Finished chain.
{'input': "When did the House episode 'Insensitive' air?", 'output': 'The "House" episode titled "Insensitive" aired on February 13, 2007.'}