Skip to content

Instantly share code, notes, and snippets.

@jordymeow
Last active March 8, 2024 14:37
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordymeow/c570826db8f72502f5f46a95cda30be5 to your computer and use it in GitHub Desktop.
Save jordymeow/c570826db8f72502f5f46a95cda30be5 to your computer and use it in GitHub Desktop.
Web Search (or similar) for AI Engine
<?php
add_filter( "mwai_context_search", 'my_web_search', 10, 3 );
function my_web_search( $context, $query, $options = [] ) {
// If the context is already provided, return it as is.
if ( ! empty( $context ) ) {
return $context;
}
// Get the latest question from the visitor.
$lastMessage = $query->get_message();
// Perform a search via the Google Custom Search API.
$apiKey = 'YOUR_API_KEY';
$engineId = 'YOUR_ENGINE_ID';
if ( empty( $apiKey ) || empty( $engineId ) ) {
return null;
}
$url = "https://www.googleapis.com/customsearch/v1?key=$apiKey&cx=$engineId&q=" . urlencode( $lastMessage );
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) ) {
return null;
}
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body );
if ( empty( $data->items ) ) {
return null;
}
// AI Engine expects a type (for logging purposes) and the content (which will be used by AI).
$context["type"] = "websearch";
$context["content"] = "";
// Loop through the 5 first results.
$max = min( count( $data->items ), 5 );
for ( $i = 0; $i < $max; $i++ ) {
$result = $data->items[$i];
$title = $result->title;
$url = $result->link;
$snippet = $result->snippet;
$content = "Title: $title\nExcerpt: $snippet\nURL: $url\n\n";
$context["content"] .= $content;
}
return $context;
}
@sid-newby
Copy link

👍

@christodeus
Copy link

Hi, I have added the snippet code and set my API and engine ID inside, however the plugin still acts as I'm not connected to the internet. Is there anything else to do to make this works ?

@christodeus
Copy link

I'm almost there but now I have this : PHP Notice: Undefined index: embeddings in /home/allezge/www/wp-content/plugins/ai-engine-pro/classes/modules/chatbot.php on line 215

@jordymeow
Copy link
Author

@christodeus It's just a notice, so you can just ignore it. But I have fixed it for next release of AI Engine :)

@christodeus
Copy link

Ok, now it works fine. I have a question, does it search first in the embedding database then on custom search if no results were found? Or does it directly search on custom search?

@jordymeow
Copy link
Author

@christodeus First, you would need to make sure your filter is ran absolutely before the embeddings. To do this, you need a higher priority than 10 (embeddings are ran with a priority of 10), which means a lower number. You could try to use 5:

add_filter( "mwai_context_search", 'my_web_search', 5, 3 );

If your filter returns a $context which is not null, the embeddings process will actually not do anything, and it is your context that will be used by the chatbot.

@Sciio
Copy link

Sciio commented Sep 25, 2023

Hello. I inserted my Google api key and engine id. My bot still won’t search the internet. Is there anything else I need to add?

@SitesByYogi
Copy link

Hi. The same thing is happening to me, I inserted my Google api key and engine id but my bot still won’t perform live searches. Any ideas?

@jordymeow
Copy link
Author

@Sciio @SitesByYogi There is an issue with AI Engine 1.9.85 and 1.9.86. But from 1.9.87, this snippet will work.

@SitesByYogi
Copy link

Thanks @jordymeow, I updated to 1.9.87 and the snippet is still not working for me. Did you manage to resolve this on your end?

@SitesByYogi
Copy link

I think I may have resolved it on my end by placing the snippet into my theme file functions.php file instead of Code Snippets plugin.

@qasimolaa
Copy link

Hi @SitesByYogi I am still experiencing the same issue with the Ai engine's internet access. These are the steps I have followed:

  1. I added the API and engine ID into the code.
  2. I added the code to my functions.php file.

However, I am still unable to access the internet.
Please let me know if I am making any mistakes.

@loopincome
Copy link

I'm currently facing challenges with the implementation of the WebSearch function, and I was hoping you could provide some guidance on its configuration.
I've observed that on Google Cloud, the number of requests is increasing, leading me to believe that the communication is functioning properly. After enabling embedding on the plugin, I noticed that with each chat request, a new line appears in the embedding section with "Status: OK, Title: N/A". However, the chat only returns a message stating that the assistant "does not have direct access to real-time news" instead of the appropriate response from Google.
I'm uncertain about a few potential configuration details:

  • Do I need to add something specific to the chat's context?
  • Are there specific settings I should configure in the embedding?

Thank you for your assistance.

@ozansrl
Copy link

ozansrl commented Dec 21, 2023

Hello. Is there anyone who has been able to do this?

@christodeus
Copy link

@jordymeow Hi Jordy, getting to you back with my old message. This code is working great, I would like just that it first search in the embeddings DB, then if nothing is found, that it search on the internet.

@genai-consultant
Copy link

@jordymeow I am getting below error PHP Fatal error: Uncaught Error: Call to undefined method Meow_MWAI_Query_Text::getLastMessage() in /var/www/html/wp-content/plugins/code-snippets/php/snippet-ops.php(582) :

@maratinn2
Copy link

I get the same error somehow the getLastMessage() is causing trouble.

@jordymeow
Copy link
Author

I have fixed the code, instead of getLastMessage, get_message should be used.

@maratinn2
Copy link

@jordymeow Hi Jordy, getting to you back with my old message. This code is working great, I would like just that it first search in the embeddings DB, then if nothing is found, that it search on the internet.

How did you manage to do it? mine is not responding the results. I must have missing something. When I check the logs I see that it works fine.

@genai-consultant
Copy link

Thanks for solution

@wgnrAI
Copy link

wgnrAI commented Mar 8, 2024

I feel like a dummy. I can't get it to work. I'm using WPCode to add snippets such as this code. Is that incorrect or is there something else I am doing wrong? Am I using the wrong ChatGPT model?

What I get is"

_"As of my last update in April 2023, I can't provide real-time information or comments from the web, including trending comments about events such as the State of the Union address or any other current events. My capabilities are centered around providing information that was available up to that point, and I don't have access to live updates or the ability to browse the internet in real-time."

  • Wágner

@wgnrAI
Copy link

wgnrAI commented Mar 8, 2024

If I am not mistaken, this:

$content = "Title: $titlenExcerpt: $snippetnURL: $urlnn";

Was recently corrected to:

$content = "Title: $title\nExcerpt: $snippet\nURL: $url\n\n";

Is that true?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment