Skip to content

Instantly share code, notes, and snippets.

@ivanmkc
Created May 25, 2023 16:00
Show Gist options
  • Save ivanmkc/ece4c6f6a31d7d5e051d82e1ab62aafa to your computer and use it in GitHub Desktop.
Save ivanmkc/ece4c6f6a31d7d5e051d82e1ab62aafa to your computer and use it in GitHub Desktop.
import os
from langchain.tools import OpenAPISpec, APIOperation
from langchain.chains import OpenAPIEndpointChain
from langchain.requests import Requests
from langchain.llms import OpenAI
spec = OpenAPISpec.from_file("nfl/specs/content.yaml")
import os
import nfl_helpers
client_key = os.environ.get("NFL_CLIENT_KEY")
client_secret = os.environ.get("NFL_CLIENT_SECRET")
token = nfl_helpers.get_token(client_key=client_key, client_secret=client_secret)
# %%
token
import os, yaml
from langchain.agents.agent_toolkits.openapi.spec import reduce_openapi_spec
with open("nfl/specs/content.yaml") as f:
content_api_spec = yaml.load(f, Loader=yaml.Loader)
content_api_spec = reduce_openapi_spec(content_api_spec)
# %%
from langchain.agents.agent_toolkits.openapi import planner
llm = OpenAI(model_name="gpt-4", temperature=0.25)
content_agent = planner.create_openapi_agent(
api_spec=content_api_spec,
requests_wrapper=Requests(headers={"Authorization": f"Bearer {token}"}),
llm=llm,
)
with open("nfl/specs/football-v2.yaml") as f:
football_api_spec = yaml.load(f, Loader=yaml.Loader)
football_api_spec = reduce_openapi_spec(football_api_spec)
football_agent = planner.create_openapi_agent(
api_spec=football_api_spec,
requests_wrapper=Requests(headers={"Authorization": f"Bearer {token}"}),
llm=llm,
)
# %%
with open("nfl/specs/team-content.yaml") as f:
team_content_spec = yaml.load(f, Loader=yaml.Loader)
team_content_spec = reduce_openapi_spec(team_content_spec)
team_content_agent = planner.create_openapi_agent(
api_spec=team_content_spec,
requests_wrapper=Requests(headers={"Authorization": f"Bearer {token}"}),
llm=llm,
)
# %%
# Import things that are needed generically
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.tools import BaseTool
from langchain.llms import OpenAI
from langchain import LLMMathChain, SerpAPIWrapper
# %%
tools = [
Tool(
name="Content",
func=content_agent.run,
description="useful for getting info about articles and promos. Input should be a fully formed question.",
),
Tool(
name="Football",
func=football_agent.run,
description="useful for football data, like persons, teams, and stats. Input should be a fully formed question.",
),
Tool(
name="Team content",
func=team_content_agent.run,
description="useful for team-produced content. This includes articles, authors, coaches, cheerleaders, events, photo galleries, staff members, videos, annd video channels.",
),
]
# %%
# Construct the agent. We will use the default agent type here.
# See documentation for a full list of options.
agent = initialize_agent(
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
# %%
agent.run("Give me 10 players playing this season (2023)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment