Integrating ChatGPT with DuckDuckGo
Tags: chatgpt, openai, machinelearning
Published: 2023-07-04 12:05:37

Earlier this morning [OpenAI turned off Bing integration in ChatGPT](https://help.openai.com/en/articles/8077698-how-do-i-use-chatgpt-browse-with-bing-to-search-the-web). A couple of hours later we released [DuckDuckGo](https://duckduckgo.com/) support for our chatbots. You're welcome 😁

You can [try it out by clicking our AI chatbot button in the bottom/right corner of this page](https://ainiro.io) and write something such as follows; **DuckDuckGo "Pixar"**

The double quotes are important, because we use these to infer what you want to send to DuckDuckGo, and what is the prompt that we use to semantically search our database, and what we're sending to ChatGPT to transform your query into an answer of some sort. The recipe for searching is as follows;

> DuckDuckGo "xyz search query here"

Currently it will only return the top 5 results, and the query towards DuckDuckGo is cached on the server for 5 minutes. But this can be modified easily.

## How it works?

When you create a training snippet for your semantic search database, you can add Hyperlambda code as an integrated part of your training snippet. This Hyperlambda code is not a part of your training snippet as you create embeddings for it. When we generate vectors for our training snippet using `text-embedding-ada-002` everything OpeAI sees is the following;

> DuckDuckGo

This results is that if you start any query towards our VSS database with the word _"DuckDuckGo"_, this snippet will bubble to the top. This training snippet looks as follows;

```
{{
.url
strings.split:x:@.arguments/*/prompt
   .:"\""
set-value:x:@.url
   strings.concat
      .:"https://html.duckduckgo.com/html/?q="
      strings.url-encode:x:@strings.split/1
unwrap:x:+/*
signal:magic.http.html2markdown-links
   url:x:@.url
   class:result__a
   max:int:5
   query:uddg
.result
set-value:x:@.result
   strings.concat
      .:"Below are the top 5 links I could find at DuckDuckGo for the query \""
      get-value:x:@strings.split/1
      .:"\":"
      .:"\r\n"
      .:"\r\n"
      get-value:x:@signal
return:x:@.result
}}
```

Notice, we could write any static text before and after the `{{}}` parts. This text would be used during vectorisation - However, for the DuckDuckGo use case, the above is sufficient. Once the above snippet is being used as context for ChatGPT, it will invoke **[strings.mixin]** for the training snippet. To see the result you can try executing the following code in your Hyperlambda Playground assuming you've created the above traning snippet for your `foo` model and vectorised it.

```
signal:magic.ai.get-context
   type:foo
   vector_model:text-embedding-ada-002
   prompt:@"DuckDuckGo ""Bill Gates"""
   threshold:float:0.6
   max_tokens:int:2000
```

This will find your DuckDuckGo training snippet, execute its Hyperlambda code, resulting in something such as the following being returned:

```
DuckDuckGo

Below are the top 5 links I could find at DuckDuckGo for the query "Bill Gates":

* [Bill Gates | Biography, Microsoft, & Facts | Britannica](https://www.britannica.com/biography/Bill-Gates)
* [Bill Gates - Wikipedia](https://en.wikipedia.org/wiki/Bill_Gates)
* [Bill Gates - Forbes](https://www.forbes.com/profile/bill-gates/)
* [Bill Gates meets privately with Xi Jinping as US-China tensions rise ...](https://www.cnn.com/2023/06/16/business/bill-gates-china-xi-jinping-visit-intl-hnk/index.html)
* [Xi Jinping meets Bill Gates in China, calls him 'an old friend'](https://www.reuters.com/world/china/chinas-president-xi-meet-with-bill-gates-beijing-state-media-2023-06-16/)
```

This is then used as a _"primer message"_ for ChatGPT or as _"context"_ to answer the question. And the question of course is;

> DuckDuckGo "Bill Gates"

## Using ANY website as an API

Our [website scraping technology](https://ainiro.io/blog/have-chatgpt-scrape-your-website) can (almost) use _any_ website as an _"API"_. You can see this from the arguments passed into the above **[magic.http.html2markdown-links]** Hyperlambda slot. The above arguments are as follows;

* __url__ - URL to fetch. This one is dynamically constructed for the above DuckDuckGo parts to retrieve HTML from DuckDuckGo with search results matching your query.
* __class__ - This works as a filter, and will _only_ return links with a CSS class name matching the specified class. This allows you to filter out any irrelevant navigation links for our DuckDuckGo example.
* __max__ - This is the maximum number of hyperlinks to return.
* __query__ - This is in case the URL you're interested in is obfuscated inside some sort of redirect URL, which is the case for DuckDuckGo. DuckDuckGo don't expose the actual URL, but rather a _"redirect URL"_, which we're able to intelligently parse from the QUERY parameters to the original URL, and return directly. For weird reasons ChatGPT refuses to return URLs towards DuckDuckGo, probably because Microsoft paid them I assume ...? 😜

The above slot of course, turns any website into Markdown, which ChatGPT happens to perfectly understand, allowing you to mix ChatGPT with the search results returned from DuckDuckGo. The end result becomes as follows.

![DuckDuckGo and ChatGPT](https://ainiro.io/assets/images/integrating-duckduckgo-with-chatgpt-2.jpeg)

Notice, we don't actually retrieve the websites DuckDuckGo returns, even though we could in theory have done this too, and used it as a part of our dynamic context to answer real time questions. However, the ability to have [ChatGPT work with live data](https://ainiro.io/blog/chatgpt-with-real-time-information) implies we can have ChatGPT work with _anything_ - Including your own local database if you wish. Something we demonstrate below.

* [Integrating ChatGPT with your local SQL database](https://ainiro.io/blog/connect-chatgpt-to-your-sql-database)

## Integrate ANYTHING with ChatGPT

All in all, this allows us to literally integrate EVERYTHING with ChatGPT, as long as you can provide us with the data we need. And we can use (almost) any website URL as an _"API"_ for read only data, injecting this into the text stream we're sending to ChatGPT, having ChatGPT work on real time data.

> You're welcome 😁

FYI: All the security concerns OpenAI used as arguments to shut off Bing support _does not apply for our solution_ 😎