Standard preparation
Standard preparation
Standard ritual:
In[]:=
$HistoryLength=0;SetDirectory[NotebookDirectory[]];
How to install the paclet
How to install the paclet
Installation from the paclet repository:
In[]:=
Map[PacletSiteUpdate]@PacletSites[];PacletInstall["KirillBelov/Internal"];PacletInstall["KirillBelov/Objects"];PacletInstall["KirillBelov/AILink"];Get["KirillBelov`AILink`"];SystemCredential["OPENAI_API_KEY"]=SystemCredential["OPENAI_API_KEY"(*yourapikeyhere*)];
Chat
Chat
How to create a chat object:
In[]:=
chat=AIChatObject "You are assistant",(*systemprompt*) "Temperature"->0.7, "Model"->"gpt-4o", "Tools"->{}
Out[]=
Complete the chat:
In[]:=
AIChatComplete[chat,"Hi!"];
Get all messages:
In[]:=
chat["Messages"]
Out[]=
rolesystem,contentYou are assistant,date,roleuser,contentHi!,date,roleassistant,contentHello! How can I assist you today?,refusalNull,date
Plugins
Plugins
The plugin should be a simple Wolfram Language function that returns a string. It can take strings, integers, and real numbers as arguments. The plugin must have a usage description. Here is a simple example of a plugin that allows the neural network to access the current time:
In[]:=
time::usage="time[] returns currend time in string form.";time[]:=Echo[DateString[],"Time tool call:"];
First I’ll ask GPT how long it is now without using the plugin:
In[]:=
DateString[]AIChatComplete[chat,"What is current date?"];chat[-1]["content"]
Out[]=
Fri 18 Oct 2024 14:21:52
Out[]=
I'm unable to provide real-time information, including the current date, as my training only goes up to October 2023 and I don't have access to live data. However, you can easily find the current date on your device or by doing a quick online search. Is there anything else I can help you with?
And then I’ll add it:
In[]:=
DateString[]chat["Tools"]={time};AIChatComplete[chat,"What is current date?"];chat[-1]["content"]
Out[]=
Fri 18 Oct 2024 14:21:53
»
Time tool call:Fri 18 Oct 2024 14:21:54
Out[]=
The current date is Friday, October 18, 2024.
As we can see the LLM now has access to the current time!
More complex example
More complex example
Now I’ll create something more complex based on plugins. I will teach the plugin to search for information on the Internet. To do this, I will use the DuckDuckGo open search and the URLRead function!
In[]:=
urlRead::usage="urlRead[url] read text from html page with address == url.Use this function when you need to get data from the specific address or site in the internet. result is a imple text.";urlRead[url_String]:=With{body=URLRead[url]["Body"]}, Echo[url,"URLRead call:"]; ImportString[body,"HTML"];
In[]:=
urlRead["https://exmaple.com"]
»
URLRead call:https://exmaple.com
Out[]=
Well hello there. Welcome to exmaple.com. Chances are you got here by mistake (example.com, anyone?)
And DuckDuckGo search:
In[]:=
duckDuckGoSearch::usage="duckDuckGoSearch[query] call DuckDuckGo search engine. ### Concise Description: duckDuckGoSearch- query must be only in English.**Purpose:**- Perform internet searches via DuckDuckGo's API, returning results in JSON format.**Key Features:**- Retrieves concise search results by minimizing redirects and unnecessary content.- Suitable for quick information retrieval without distractions like ads.**Usage:**- Facilitates streamlined search operations for precise and actionable data in LLM environments.";duckDuckGoSearch[query_String]:=Moduleurl="https://lite.duckduckgo.com/lite/", request,response,responseBody, Echo[query,"DDGS call:"]; request=HTTPRequesturl,<| Method->"POST","ContentType"->"application/x-www-form-urlencoded", "Body"->{"q"->query} |>; response=URLRead[request]; responseBody=response["Body"]; ImportString[ExportString[responseBody,"String"],"HTML"]<>"\n\n"<> "https://duckduckgo.com?"<>URLQueryEncode[{"q"->query}];
In[]:=
StringTake[duckDuckGoSearch["Wolfram Language"],{750,1225}]
»
DDGS call:Wolfram Language
Out[]=
Zero-click info: Wolfram Language The Wolfram Language is a proprietary, general, very high-level multi-paradigm programming language developed by Wolfram Research. It emphasizes symbolic computation, functional programming, and rule-based programming and can employ arbitrary structures and data. It is the programming language of the mathematical symbolic computation program Mathematica. More at Wikipedia 1. Official site Wolfram Language www.wolfram.com
I will now add these plugins to the chat:
In[]:=
chat["Tools"]={time,urlRead,duckDuckGoSearch};
And I’ll ask the bot to look something up on the Internet:
In[]:=
AIChatComplete[chat,"Can you find article with last Donald Trump interview?Find link to the post about it, read and analyse"];chat[-1]["content"]
That’s fantastic! Now LLM searches the internet for information on its own, follows links and reads news. And at my request the bot can analyze everything it has read! Of course, in this way I can give the neural network access not only to the Internet, but also, for example, to the documentation page of some project, so that it can learn how to use it. I can also give access to file folders on my computer, allow it to write and execute code, save and edit files, and all this with a set of simple Wolfram Language functions! Anyway, I use this thing all the time now and thought I’d share it with the community. Thank you all for your attention!
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
AILink paclet: Wolfram + GPT-4o integration with plugins, web and system access, and data analysis
by Kirill Belov
Wolfram Community, STAFF PICKS, October 18, 2024
https://community.wolfram.com/groups/-/m/t/3300846
by Kirill Belov
Wolfram Community, STAFF PICKS, October 18, 2024
https://community.wolfram.com/groups/-/m/t/3300846