Skip to content

Commit

Permalink
Added search command for fetching from DuckDuckGo
Browse files Browse the repository at this point in the history
  • Loading branch information
isKONSTANTIN committed Jan 28, 2025
1 parent 51c0cf1 commit 91faf56
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Knst's Telegram AI Bot is designed to enhance your experience with GPT models an

- 🎭 Telegram Reactions: Dynamic interactions using message reactions.

- 🌐 Web Content Integration: Automatically extract text from web pages without explicit commands.
- 🌐 Web Content Integration: Automatically extract text from web pages without explicit commands and searching via DuckDuckGo

- 🎨 DALL·E 3 Support: Create imaginative images using the DALL·E 3.

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'su.knst.telegram.ai'
version = '1.6.1'
version = '1.7.0'
mainClassName = group + ".Main"
archivesBaseName = 'Knst-Telegram-AI'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public static CompletableFuture<String> downloadFileAsString(String url) {
return Files.readString(f.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
Files.deleteIfExists(f.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package su.knst.telegram.ai.utils.functions;

import su.knst.telegram.ai.utils.functions.search.SearchInfo;

import java.util.List;

public class FunctionSearchResult extends FunctionResult {
public List<SearchInfo> searchInfos;

public FunctionSearchResult(List<SearchInfo> searchInfos) {
super(true);

this.searchInfos = searchInfos;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package su.knst.telegram.ai.utils.functions.search;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class DDGSearch {
protected static final String url = "https://html.duckduckgo.com/html/?q=";

public static List<SearchInfo> search(String request) throws IOException {
Document doc =Jsoup.connect(url + URLEncoder.encode(request, Charset.defaultCharset())).get();
Elements results = doc.body().getElementsByClass("result");

return parseInfo(results);
}

protected static List<SearchInfo> parseInfo(Elements elements) {
ArrayList<SearchInfo> searchInfos = new ArrayList<>();

for (Element element : elements) {
SearchInfo searchInfo = new SearchInfo();

Elements title = element.getElementsByClass("result__title");
Elements url = element.getElementsByClass("result__url");
Elements description = element.getElementsByClass("result__snippet");

if (!title.isEmpty())
searchInfo.setTitle(title.text());

if (!url.isEmpty())
searchInfo.setUrl(url.get(0).attributes().get("href"));

if (!description.isEmpty())
searchInfo.setDescription(description.text());

searchInfos.add(searchInfo);
}

return searchInfos;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package su.knst.telegram.ai.utils.functions.search;

public class SearchInfo {
protected String title;
protected String url;
protected String description;

public SearchInfo() {
}

public SearchInfo(String title, String url, String description) {
this.title = title;
this.url = url;
this.description = description;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@Override
public String toString() {
return "SearchInfo{" +
"title='" + title + '\'' +
", url='" + url + '\'' +
", description='" + description + '\'' +
'}';
}
}
14 changes: 14 additions & 0 deletions src/main/java/su/knst/telegram/ai/workers/AiTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import su.knst.telegram.ai.managers.AiContextManager;
import su.knst.telegram.ai.managers.AiMessagesManager;
import su.knst.telegram.ai.utils.functions.*;
import su.knst.telegram.ai.utils.functions.search.DDGSearch;
import su.knst.telegram.ai.utils.parsers.Markdown2DocxConverter;
import su.knst.telegram.ai.utils.parsers.TextConverters;

Expand Down Expand Up @@ -112,6 +113,19 @@ protected void buildTools() {
Parameter.of("url", "string", "Url to read", true)
);

function("search", "Use this function to search websites from duckduckgo",
(chatId, contextId, args) -> {
String request = args.get("request");

try {
return new FunctionSearchResult(DDGSearch.search(request));
} catch (Exception e) {
return new FunctionError("Fail to search");
}
},
Parameter.of("request", "string", "Search request", true)
);

function("imagine", "Use this function to imagine and send photo to user using DALL·E 3. Do NOT share result link",
(chatId, contextId, args) -> {
String prompt = args.get("prompt");
Expand Down

0 comments on commit 91faf56

Please sign in to comment.