Skip to content

Instantly share code, notes, and snippets.

@virattt
Created February 17, 2024 19:49
Show Gist options
  • Save virattt/ab6667e0a456140ee4bf64e0fddc7d3b to your computer and use it in GitHub Desktop.
Save virattt/ab6667e0a456140ee4bf64e0fddc7d3b to your computer and use it in GitHub Desktop.
query_expansion-cohere.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMfzIkfDBE97PV3PHB3IwFi",
"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/virattt/ab6667e0a456140ee4bf64e0fddc7d3b/query_expansion-cohere.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "HRA6TPl0lXqq"
},
"outputs": [],
"source": [
"!pip install cohere"
]
},
{
"cell_type": "code",
"source": [
"import getpass\n",
"import os\n",
"\n",
"# Set your Cohere API key\n",
"os.environ[\"COHERE_API_KEY\"] = getpass.getpass()"
],
"metadata": {
"id": "4fJZ05n9lflZ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"query = \"What are the revenues of Airbnb, Booking, and Expedia?\""
],
"metadata": {
"id": "5UbW7pydl82v"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Use `command` model for query expansion"
],
"metadata": {
"id": "025xZuDOlnT6"
}
},
{
"cell_type": "code",
"source": [
"import cohere\n",
"\n",
"# Get your cohere API key on: www.cohere.com\n",
"co = cohere.Client(os.environ[\"COHERE_API_KEY\"])\n",
"\n",
"# Call the model\n",
"response = co.chat(\n",
" message=query,\n",
" search_queries_only=True,\n",
")"
],
"metadata": {
"id": "b8-Ua66vlsWJ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Print the rewritten query\n",
"rewritten_queries = [query.get('text', None) for query in response.search_queries]\n",
"print(rewritten_queries)"
],
"metadata": {
"id": "QTXJByrxl3E_"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Use `command-light` model for query expansion"
],
"metadata": {
"id": "oYnx5WtEmXz7"
}
},
{
"cell_type": "code",
"source": [
"# Get your cohere API key on: www.cohere.com\n",
"co = cohere.Client(os.environ[\"COHERE_API_KEY\"])\n",
"\n",
"# Call the model\n",
"response = co.chat(\n",
" model='command-light',\n",
" message=query,\n",
" search_queries_only=True,\n",
")"
],
"metadata": {
"id": "lNXcXI9ymJZa"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Print the rewritten query\n",
"rewritten_queries = [query.get('text', None) for query in response.search_queries]\n",
"print(rewritten_queries)"
],
"metadata": {
"id": "MaFJ6lrbmfmQ"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment