- ‚Äòopenapi.yaml‚Äô saved [286747/286747]    from langchain.agents.agent_toolkits.openapi.spec import reduce_openapi_specwith open("openai_openapi.yaml") as f:    raw_openai_api_spec = yaml.load(f, Loader=yaml.Loader)openai_api_spec = reduce_openapi_spec(raw_openai_api_spec)with open("klarna_openapi.yaml") as f:    raw_klarna_api_spec = yaml.load(f, Loader=yaml.Loader)klarna_api_spec = reduce_openapi_spec(raw_klarna_api_spec)with open("spotify_openapi.yaml") as f:    raw_spotify_api_spec = yaml.load(f, Loader=yaml.Loader)spotify_api_spec = reduce_openapi_spec(raw_spotify_api_spec)We'll work with the Spotify API as one of the examples of a somewhat complex API. There's a bit of auth-related setup to do if you want to replicate this.You'll have to set up an application in the Spotify developer console, documented here, to get credentials: CLIENT_ID, CLIENT_SECRET, and REDIRECT_URI.To get an access tokens (and keep them fresh), you can implement the oauth flows, or you can use spotipy. If you've set your Spotify creedentials as environment variables SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, and SPOTIPY_REDIRECT_URI, you can use the helper functions below:import spotipy.util as utilfrom langchain.requests import RequestsWrapperdef construct_spotify_auth_headers(raw_spec: dict):    scopes = list(        raw_spec["components"]["securitySchemes"]["oauth_2_0"]["flows"][            "authorizationCode"        ]["scopes"].keys()    )    access_token = util.prompt_for_user_token(scope=",".join(scopes))    return {"Authorization": f"Bearer {access_token}"}# Get API credentials.headers = construct_spotify_auth_headers(raw_spotify_api_spec)requests_wrapper = RequestsWrapper(headers=headers)How big is this spec?‚Äãendpoints = [    (route, operation)    for route, operations in raw_spotify_api_spec["paths"].items()    for operation in operations    if operation in ["get", "post"]]len(endpoints)    63import tiktokenenc = tiktoken.encoding_for_model("text-davinci-003")def
We can construct agents to consume arbitrary APIs, here APIs conformant to the OpenAPI/Swagger specification.
We can construct agents to consume arbitrary APIs, here APIs conformant to the OpenAPI/Swagger specification. ->: - ‚Äòopenapi.yaml‚Äô saved [286747/286747]    from langchain.agents.agent_toolkits.openapi.spec import reduce_openapi_specwith open("openai_openapi.yaml") as f:    raw_openai_api_spec = yaml.load(f, Loader=yaml.Loader)openai_api_spec = reduce_openapi_spec(raw_openai_api_spec)with open("klarna_openapi.yaml") as f:    raw_klarna_api_spec = yaml.load(f, Loader=yaml.Loader)klarna_api_spec = reduce_openapi_spec(raw_klarna_api_spec)with open("spotify_openapi.yaml") as f:    raw_spotify_api_spec = yaml.load(f, Loader=yaml.Loader)spotify_api_spec = reduce_openapi_spec(raw_spotify_api_spec)We'll work with the Spotify API as one of the examples of a somewhat complex API. There's a bit of auth-related setup to do if you want to replicate this.You'll have to set up an application in the Spotify developer console, documented here, to get credentials: CLIENT_ID, CLIENT_SECRET, and REDIRECT_URI.To get an access tokens (and keep them fresh), you can implement the oauth flows, or you can use spotipy. If you've set your Spotify creedentials as environment variables SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, and SPOTIPY_REDIRECT_URI, you can use the helper functions below:import spotipy.util as utilfrom langchain.requests import RequestsWrapperdef construct_spotify_auth_headers(raw_spec: dict):    scopes = list(        raw_spec["components"]["securitySchemes"]["oauth_2_0"]["flows"][            "authorizationCode"        ]["scopes"].keys()    )    access_token = util.prompt_for_user_token(scope=",".join(scopes))    return {"Authorization": f"Bearer {access_token}"}# Get API credentials.headers = construct_spotify_auth_headers(raw_spotify_api_spec)requests_wrapper = RequestsWrapper(headers=headers)How big is this spec?‚Äãendpoints = [    (route, operation)    for route, operations in raw_spotify_api_spec["paths"].items()    for operation in operations    if operation in ["get", "post"]]len(endpoints)    63import tiktokenenc = tiktoken.encoding_for_model("text-davinci-003")def