Skip to content

Instantly share code, notes, and snippets.

@donvito
Last active April 24, 2024 12:44
Show Gist options
  • Save donvito/cb67bc5a497fd4bb02b5715c6096c0c0 to your computer and use it in GitHub Desktop.
Save donvito/cb67bc5a497fd4bb02b5715c6096c0c0 to your computer and use it in GitHub Desktop.
api_chain_one_map_api_example
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyM7v7Y/YGzZ1AdyKFSGUa3i",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/donvito/cb67bc5a497fd4bb02b5715c6096c0c0/api_chain_example.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"## **API Chain Example**"
],
"metadata": {
"id": "qiW01_EEOr2w"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "kXUUiXToOQf0"
},
"outputs": [],
"source": [
"pip install langchain"
]
},
{
"cell_type": "markdown",
"source": [
"## **Sample code**"
],
"metadata": {
"id": "lgrCo5DNOvAi"
}
},
{
"cell_type": "code",
"source": [
"import os\n",
"\n",
"from langchain.chains import APIChain\n",
"from langchain.chat_models import ChatOpenAI\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
"\n",
"llm = ChatOpenAI(temperature=0, model_name=\"gpt-3.5-turbo\", max_tokens=256, verbose=True)\n",
"\n",
"apiSpec = \"\"\"API documentation:\n",
"Base URL: https://developers.onemap.sg/commonapi\n",
"Endpoint: /search\n",
"Example API call: https://developers.onemap.sg/commonapi/search?searchVal=520201&returnGeom=Y&getAddrDetails=Y&pageNum=1\n",
"\n",
"This API is for retrieving address details based on a search keyword.\n",
"\n",
"Request POST payload (JSON object) \n",
"Json Field\tFormat\tRequired\tDefault\tDescription\n",
"searchVal\tString\tNo\t\tAny string representing a steet address, establishment, location or postal code\n",
"returnGeom String Yes N always set to N\n",
"getAddrDetails String Yes Y always set to Y\n",
"\n",
"INSTRUCTIONS FOR RESPONDING\n",
"Reply to the user with the first ADDRESS result, respond in natural language indicate the ADDRESS from the APi response. If data is empty, just say you the search has not returned any results and you are sorry.\n",
"\n",
"\"\"\"\n",
"\n",
"# set the headers if you need to do add authorization, for this example we don't need it because the API is public\n",
"# apiToken = \"\"\n",
"# headers = {\"Authorization\": f\"Bearer {apiToken}\"} \n",
"# chain_new = APIChain.from_llm_and_api_docs(llm, apiSpec, verbose=True, headers=headers)\n",
"\n",
"chain_new = APIChain.from_llm_and_api_docs(llm, apiSpec, verbose=True)\n",
"response = chain_new.run('What is the postal code of Our Tampines Hub?')\n",
"print(response)"
],
"metadata": {
"id": "retw0t48T55a"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "WUgESIhcZfRl"
},
"execution_count": 2,
"outputs": []
}
]
}
@CHIRU98
Copy link

CHIRU98 commented Jan 31, 2024

Hi, i am getting the error like this after running the code " Please provide a list of domains to limit access using limit_to_domains. (type=value_error)".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment