Before You Get Started

To use the You.com Search API, you will need an API key. Please visit api.you.com for more details.

Enriching Cohere’s LLM with the You.com Search API

Follow these steps to query You.com’s Search API and enrich LLM responses with our snippets. You will need to have the environment variables AUTH_KEY and COHERE_API_KEY set to follow along with this guide. This example uses the requests library, you can alternatively look at using our LangChain retriever.

1. Install

pip install cohere

2. Create the Cohere Client

main.py
import os

if not os.getenv("AUTH_KEY") or not os.getenv("COHERE_API_KEY"):
  raise RuntimeError("You need to set both AUTH_KEY and COHERE_API_KEY environment variables to proceed")

try:
  import cohere
except:
  raise RuntimeError("Cohere library is not installed")

cohere_client = cohere.Client(os.getenv("COHERE_API_KEY"))

3. Create a function to call You.com’s Search API

This function will be used to augment Cohere’s LLM generations.

main.py
import requests

def get_ai_snippets_for_query(query):
    headers = {"X-API-Key": os.environ["AUTH_KEY"]}
    results = requests.get(
        f"https://api.ydc-index.io/search?query={query}",
        headers=headers,
    ).json()

    # We return many text snippets for each search hit so
    # we need to explode both levels
    return "\n".join(["\n".join(hit["snippets"]) for hit in results["hits"]])

4. Calling Cohere’s API to generate LLM responses

Here, we’ll define two functions. get_cohere_prompt takes a query and context (provided by You.com’s Search API) to create an enriched prompt. ask_cohere uses the prompt to call the Cohere API.

main.py
def get_cohere_prompt(query, context):
    return f"""given a question and a bunch of snippets context try to answer the question using the context. If you can't please say 'Sorry hooman, no dice'.
question: {query}
context: {context}
answer: """

def ask_cohere(query, context):
    try:
        return cohere_client.generate(prompt=get_cohere_prompt(query, context))[
            0
        ].text
    except Exception as e:
        print(
            "Cohere call failed for query {} and context {}".format(query, context)
        )
        print(e)
        return "Sorry hooman, no dice"

5. Putting it all together

Now that we are able to call You.com’s Search API and Cohere’s LLM, we want to pass the Search API’s rich snippets into the prompt.

main.py
def ask_cohere_with_ai_snippets(query):
    ai_snippets = get_ai_snippets_for_query(query)
    return ask_cohere(query, ai_snippets)

Now, you’ll be able to leverage You.com’s powerful Search API to enrich your LLM responses!

Try it yourself

main.py
import os

if not os.getenv("AUTH_KEY") or not os.getenv("COHERE_API_KEY"):
  raise RuntimeError("You need to set both AUTH_KEY and COHERE_API_KEY environment variables to proceed")

try:
  import cohere
except:
  raise RuntimeError("Cohere library is not installed")

cohere_client = cohere.Client(os.getenv("COHERE_API_KEY"))

import requests

def get_ai_snippets_for_query(query):
    headers = {"X-API-Key": os.environ["AUTH_KEY"]}
    results = requests.get(
        f"https://api.ydc-index.io/search?query={query}",
        headers=headers,
    ).json()

    # We return many text snippets for each search hit so
    # we need to explode both levels
    return "\n".join(["\n".join(hit["snippets"]) for hit in results["hits"]])

def get_cohere_prompt(query, context):
    return f"""given a question and a bunch of snippets context try to answer the question using the context. If you can't please say 'Sorry hooman, no dice'.
question: {query}
context: {context}
answer: """

def ask_cohere(query, context):
    try:
        return cohere_client.generate(prompt=get_cohere_prompt(query, context))[
            0
        ].text
    except Exception as e:
        print(
            "Cohere call failed for query {} and context {}".format(query, context)
        )
        print(e)
        return "Sorry hooman, no dice"

def ask_cohere_with_ai_snippets(query):
    ai_snippets = get_ai_snippets_for_query(query)
    return ask_cohere(query, ai_snippets)