Function Repository Resource:

SummarizeDefinition

Source Notebook

Generate the definition of a symbol using natural language

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["SummarizeDefinition"][symbol]

explains the definition of symbol using natural language.

ResourceFunction["SummarizeDefinition"][symbol,fmt]

formats the summary in a format specified by fmt.

Details and Options

ResourceFunction["SummarizeDefinition"] requires an API key from OpenAI (see the link for instructions on how to obtain an API key).
Storing the API key with SystemCredential["OPENAI_API_KEY"]="key" will make the key persistently available to ResourceFunction["SummarizeDefinition"].
ResourceFunction["SummarizeDefinition"] has the attribute HoldFirst.
Some possible values for fmt are:
Nonedo not apply any formatting to the summary string
Automaticautomatically format inline code delimited with "`" characters
"TextCell"gives a Cell with automatic formatting applied that can be inserted into a notebook
The default value for fmt is Automatic.
ResourceFunction["SummarizeDefinition"] accepts the following options:
"OpenAIKey"SystemCredential["OPENAI_API_KEY"]the API key used to authenticate requests with OpenAI
"Model""text-davinci-003"the OpenAI language model used to generate summary text
"MaxTokens"2048maximum number of tokens to use
Additional model names can be found on the OpenAI Models documentation page.

Examples

Basic Examples (2) 

Explain what a function does using natural language:

In[1]:=
AddOne[x_] := x + 1
In[2]:=
ResourceFunction["SummarizeDefinition"][AddOne]
Out[2]=

Please document my function for me:

In[3]:=
ResourceFunction["SummarizeDefinition"][
ResourceFunction["SummarizeDefinition"]]
Out[3]=

Scope (4) 

Summarize the definition of a ResourceFunction:

In[4]:=
ResourceFunction["SummarizeDefinition"][ResourceFunction["GrayCode"]]
Out[4]=

Do not apply any formatting to the result:

In[5]:=
ResourceFunction["SummarizeDefinition"][
 ResourceFunction["BettiNumbers"], None]
Out[5]=

Format as a text cell:

In[6]:=
ResourceFunction["SummarizeDefinition"][
  ResourceFunction["PartyParrot"], "TextCell"] // CellPrint

PartyParrot is a Wolfram Language function that retrieves data about various Party Parrots from an external source. It can be used with or without arguments. When called without arguments, it will return a list of all the Party Parrot names. When called with a single argument - the id of the Party Parrot - it will return the simple animation associated with that Party Parrot.


Summarize the definition of a PacletSymbol:

In[7]:=
ResourceFunction["SummarizeDefinition"][
PacletSymbol[
 "ChristopherWolfram/OpenAILink", "ChristopherWolfram`OpenAILink`OpenAICompletion"]]
Out[7]=

Options (7) 

OpenAIKey (3) 

Specify the OpenAI API key to authenticate requests with:

In[8]:=
AddTwo[x_] := x + 2;
In[9]:=
ResourceFunction["SummarizeDefinition"][AddTwo, "OpenAIKey" -> "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"]
Out[9]=

Save the API key persistently using SystemCredential:

In[10]:=
SystemCredential[
  "OPENAI_API_KEY"] = "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"

This key is now used by default:

In[11]:=
ResourceFunction["SummarizeDefinition"][AddTwo]
Out[11]=

Model (2) 

Specify an alternate language model:

In[12]:=
ResourceFunction["SummarizeDefinition"][
 ResourceFunction["ColorToHex"], "Model" -> "gpt-3.5-turbo"]
Out[12]=

GPT-4 tends to yield more detailed responses, but is much slower than other models:

In[13]:=
AbsoluteTiming[
 ResourceFunction["SummarizeDefinition"][
  ResourceFunction["ColorToHex"], "Model" -> "gpt-4"]]
Out[13]=
In[14]:=
AbsoluteTiming[
 ResourceFunction["SummarizeDefinition"][
  ResourceFunction["ColorToHex"], "Model" -> "text-davinci-003"]]
Out[14]=

MaxTokens (2) 

Some models require a smaller number of tokens:

In[15]:=
ResourceFunction["SummarizeDefinition"][
 ResourceFunction["NestedLookup"], "Model" -> "davinci-instruct-beta"]
Out[15]=

Specify a smaller value:

In[16]:=
ResourceFunction["SummarizeDefinition"][
 ResourceFunction["NestedLookup"], "Model" -> "davinci-instruct-beta",
  "MaxTokens" -> 1024]
Out[16]=

Properties and Relations (5) 

SummarizeDefinition has the attribute HoldFirst, so it can be used on symbols defined with OwnValues:

In[17]:=
$RandomNumber := RandomReal[{1, 10}]
In[18]:=
ResourceFunction["SummarizeDefinition"][$RandomNumber]
Out[18]=

SummarizeDefinition is not deterministic:

In[19]:=
AddOne[x_] := x + 1
In[20]:=
Column[Table[ResourceFunction["SummarizeDefinition"][AddOne], 3], Spacings -> 2]
Out[20]=

Symbol names have a significant influence on the result. Here are two equivalent definitions that only differ in name (the definition is from the resource function StupidSort):

In[21]:=
StupidSort[list_] := list //. {x___, a_, b_, y___} /; a > b :> {x, b, a, y}
ReallyAwesomeSort[list_] := list //. {x___, a_, b_, y___} /; a > b :> {x, b, a, y}

SummarizeDefinition is far more likely to mention that StupidSort is inefficient:

In[22]:=
ResourceFunction["SummarizeDefinition"][StupidSort]
Out[22]=
In[23]:=
Column[Table[
  ResourceFunction["SummarizeDefinition"][ReallyAwesomeSort], 3], Spacings -> 2]
Out[23]=

Well-named symbols will often lead to higher-level explanations:

In[24]:=
g[n_Integer?NonNegative] := IntegerDigits[BitXor[n, BitShiftRight[n, 1]], 2];
In[25]:=
ResourceFunction["SummarizeDefinition"][g]
Out[25]=
In[26]:=
GrayCode[n_Integer?NonNegative] := IntegerDigits[BitXor[n, BitShiftRight[n, 1]], 2];
In[27]:=
ResourceFunction["SummarizeDefinition"][GrayCode]
Out[27]=

SummarizeDefinition includes dependent symbol definitions:

In[28]:=
MyFunction[x_] := f[x];
f[x_] := x + 1;
In[29]:=
ResourceFunction["SummarizeDefinition"][MyFunction]
Out[29]=

Possible Issues (4) 

SummarizeDefinition will not work without an OpenAI API key:

In[30]:=
oldKey = SystemCredential["OPENAI_API_KEY"];
SystemCredential["OPENAI_API_KEY"] =.
In[31]:=
ResourceFunction["SummarizeDefinition"][ResourceFunction["GrayCode"]]
Out[31]=
In[32]:=
SystemCredential["OPENAI_API_KEY"] = oldKey;
In[33]:=
ResourceFunction["SummarizeDefinition"][ResourceFunction["GrayCode"]]
Out[33]=

System symbols are not supported:

In[34]:=
ResourceFunction["SummarizeDefinition"][CloudDeploy]
Out[34]=

Symbols cannot have the ReadProtected attribute:

In[35]:=
MyFunction[x_] := x + 1;
SetAttributes[MyFunction, ReadProtected];
In[36]:=
ResourceFunction["SummarizeDefinition"][MyFunction]
Out[36]=

Some models can occasionally return an empty response:

In[37]:=
ResourceFunction["SummarizeDefinition"][ResourceFunction["GrayCode"], "Model" -> "davinci-instruct-beta", "MaxTokens" -> 1024]
Out[37]=

Some are just not suitable for this particular task:

In[38]:=
Snippet[ResourceFunction["SummarizeDefinition"][
  ResourceFunction["GrayCode"], None, "Model" -> "babbage", "MaxTokens" -> 1024], 10]
Out[38]=

Version History

  • 1.1.0 – 03 April 2023
  • 1.0.0 – 20 March 2023

Related Resources

License Information