{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "245a954a",
      "metadata": {
        "id": "245a954a"
      },
      "source": [
        "# Golden Query\n",
        "\n",
        ">[Golden](https://golden.com) provides a set of natural language APIs for querying and enrichment using the Golden Knowledge Graph e.g. queries such as: `Products from OpenAI`, `Generative ai companies with series a funding`, and `rappers who invest` can be used to retrieve structured data about relevant entities.\n",
        ">\n",
        ">The `golden-query` langchain tool is a wrapper on top of the [Golden Query API](https://docs.golden.com/reference/query-api) which enables programmatic access to these results.\n",
        ">See the [Golden Query API docs](https://docs.golden.com/reference/query-api) for more information.\n",
        "\n",
        "\n",
        "This notebook goes over how to use the `golden-query` tool.\n",
        "\n",
        "- Go to the [Golden API docs](https://docs.golden.com/) to get an overview about the Golden API.\n",
        "- Get your API key from the [Golden API Settings](https://golden.com/settings/api) page.\n",
        "- Save your API key into GOLDEN_API_KEY env variable"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "34bb5968",
      "metadata": {
        "id": "34bb5968"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "os.environ[\"GOLDEN_API_KEY\"] = \"\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ac4910f8",
      "metadata": {
        "id": "ac4910f8"
      },
      "outputs": [],
      "source": [
        "from langchain.utilities.golden_query import GoldenQueryAPIWrapper"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "84b8f773",
      "metadata": {
        "id": "84b8f773"
      },
      "outputs": [],
      "source": [
        "golden_query = GoldenQueryAPIWrapper()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "068991a6",
      "metadata": {
        "id": "068991a6",
        "outputId": "c5cdc6ec-03cf-4084-cc6f-6ae792d91d39"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'results': [{'id': 4673886,\n",
              "   'latestVersionId': 60276991,\n",
              "   'properties': [{'predicateId': 'name',\n",
              "     'instances': [{'value': 'Samsung', 'citations': []}]}]},\n",
              "  {'id': 7008,\n",
              "   'latestVersionId': 61087416,\n",
              "   'properties': [{'predicateId': 'name',\n",
              "     'instances': [{'value': 'Intel', 'citations': []}]}]},\n",
              "  {'id': 24193,\n",
              "   'latestVersionId': 60274482,\n",
              "   'properties': [{'predicateId': 'name',\n",
              "     'instances': [{'value': 'Texas Instruments', 'citations': []}]}]},\n",
              "  {'id': 1142,\n",
              "   'latestVersionId': 61406205,\n",
              "   'properties': [{'predicateId': 'name',\n",
              "     'instances': [{'value': 'Advanced Micro Devices', 'citations': []}]}]},\n",
              "  {'id': 193948,\n",
              "   'latestVersionId': 58326582,\n",
              "   'properties': [{'predicateId': 'name',\n",
              "     'instances': [{'value': 'Freescale Semiconductor', 'citations': []}]}]},\n",
              "  {'id': 91316,\n",
              "   'latestVersionId': 60387380,\n",
              "   'properties': [{'predicateId': 'name',\n",
              "     'instances': [{'value': 'Agilent Technologies', 'citations': []}]}]},\n",
              "  {'id': 90014,\n",
              "   'latestVersionId': 60388078,\n",
              "   'properties': [{'predicateId': 'name',\n",
              "     'instances': [{'value': 'Novartis', 'citations': []}]}]},\n",
              "  {'id': 237458,\n",
              "   'latestVersionId': 61406160,\n",
              "   'properties': [{'predicateId': 'name',\n",
              "     'instances': [{'value': 'Analog Devices', 'citations': []}]}]},\n",
              "  {'id': 3941943,\n",
              "   'latestVersionId': 60382250,\n",
              "   'properties': [{'predicateId': 'name',\n",
              "     'instances': [{'value': 'AbbVie Inc.', 'citations': []}]}]},\n",
              "  {'id': 4178762,\n",
              "   'latestVersionId': 60542667,\n",
              "   'properties': [{'predicateId': 'name',\n",
              "     'instances': [{'value': 'IBM', 'citations': []}]}]}],\n",
              " 'next': 'https://golden.com/api/v2/public/queries/59044/results/?cursor=eyJwb3NpdGlvbiI6IFsxNzYxNiwgIklCTS04M1lQM1oiXX0%3D&pageSize=10',\n",
              " 'previous': None}"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "import json\n",
        "\n",
        "json.loads(golden_query.run(\"companies in nanotech\"))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": ".venv",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.13"
    },
    "vscode": {
      "interpreter": {
        "hash": "53f3bc57609c7a84333bb558594977aa5b4026b1d6070b93987956689e367341"
      }
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}