Published :
May 23, 2024

Use OpenAI's API Without Your Own API Key

One of the beloved features of Neptyne is the fact that we allow you to use 5 APIs for free. No need to register or mess around with API keys. Today we are opening this feature up to our Google Sheets API. This means you can now write python code in Google Sheets that interacts with OpenAI and call that function directly from your spreadsheet!

For a quick demo of this checkout this sheet. It combines the Web search API with the AI API to create a research tool where you put in a research topic and it gives you results based on a web search summarized by the AI.

import requests
import neptyne as nt
from openai import OpenAI

BING_ENDPOINT = "https://api.bing.microsoft.com/v7.0/search"


def web_search(q):
    params = {"q": q, "count": 10, "mkt": "en-US"}
    response = requests.get(BING_ENDPOINT, params=params)
    response.raise_for_status()
    return [
        [rec.get('url'), rec.get('name'), rec.get('snippet')]
        for rec in response.json()['webPages']['value']
    ]


def summarize_results(query, titles, snippets):
    results = [
        title + ":" + snippet
        for title, snippet in zip(titles, snippets)
        if title and snippet
    ]
    if not results:
        return ""

    prompt = (
        "Formulate a 100 word answer to this question\n"
        + query
        + "\n"
        + "Based on these web search results (titles and snippets):\n"
        + "\n".join(results)
    )
    # No key needed; Neptyne provides one:
    client = OpenAI()

    completion = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "assistant",
                "content": prompt,
            },
        ],
    )
    return completion.choices[0].message.content

How does this work? It’s easy. You just call the API as you always would, but there is no need to provide a token in the headers, url or wherever the API expects it. The Neptyne environment will detect that you are trying to access the API and add a token on the fly. There's a limit to what you can do; we give you a bit of quota to play around with. You'll need a paid plan to go beyond that but if you built something cool, ping us and we can make an exception.

Which APIs can you use for free today?

  • OpenAI. This is not new but still a great way to get started with building AI apps
  • Web search. If you build lang-chain type of applications asking the AI to generate search queries to find detailed information is a powerful pattern. Currently we’re using Bing
  • Web page rendering. Scraping the web pages by just getting the html doesn’t cut it anymore in today's web. PhantomJsCloud renders pages in a browser, executing javascript and everything. You can now use this directly from your favorite spreadsheet.
  • Geocoding. If your AI has suggested some web searches and you’ve successfully gotten the contents of the results, maybe you want to show things on a map. Use Google’s geocoder without a key to get started.
  • Financial information. Lots of people use Neptyne to build financial applications. The iexfinance API gives you access to a wealth of financial information - from real time quotes, to crypto to historical information.

We believe that this is one of the most powerful features across any Google Sheets Add-ons available today and we can’t wait to see what you end up building. And of course if there is an API we currently do not support, let us know and we’ll have a look to see if we can add it.