-
See Also
- LLMSynthesize
-
- Service Connections
- OpenAI
- Anthropic
- GoogleGemini
- Cohere
- DeepSeek
- Groq
- MistralAI
- TogetherAI
-
-
See Also
- LLMSynthesize
-
- Service Connections
- OpenAI
- Anthropic
- GoogleGemini
- Cohere
- DeepSeek
- Groq
- MistralAI
- TogetherAI
-
See Also
"xAI" (Service Connection)
Connecting & Authenticating
Requests
"TestConnection" — returns Success for working connection, Failure otherwise
"Chat" — create a response for the given chat conversation
| "Messages" | (required) | a list of messages in the conversation, each given as an association with "Role" and "Content" keys | |
| "FrequencyPenalty" | Automatic | penalize tokens based on their existing frequency in the text so far (between -2 and 2) | |
| "LogProbs" | Automatic | include the log probabilities on the most likely tokens, as well as the chosen tokens (between 0 and 5) | |
| "MaxTokens" | Automatic | maximum number of tokens to generate | |
| "Model" | Automatic | name of the model to use | |
| "N" | Automatic | number of chat completions to return | |
| "PresencePenalty" | Automatic | penalize new tokens based on whether they appear in the text so far (between -2 and 2) | |
| "StopTokens" | None | up to four strings where the API will stop generating further tokens | |
| "Stream" | Automatic | return the result as server-sent events | |
| "Temperature" | Automatic | sampling temperature (between 0 and 2) | |
| "ToolChoice" | Automatic | which (if any) tool is called by the model | |
| "Tools" | Automatic | one or more LLMTool objects available to the model | |
| "TotalProbabilityCutof" | None | an alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with the requested probability mass | |
| "User" | Automatic | unique identifier representing the end user |
"ImageCreate" — create a square image given a prompt
| "Prompt" | (required) | text description of the desired image | |
| "Model" | Automatic | name of the model to use | |
| "N" | Automatic | number of images to generate | |
| "Quality" | Automatic | control the quality of the result; possible values include "hd" | |
| "User" | Automatic | unique identifier representing the end user |
Model Lists
"ChatModelList" — list models available for the "Chat" request
"ImageModelList" — list models available for the image-related requests
Examples
open all close allBasic Examples (1)
xAI = ServiceConnect["xAI"]Generate a response from a chat:
ServiceExecute[xAI, "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "hello there"|>}}]Generate an Image from a prompt:
ServiceExecute[xAI, "ImageCreate", {"Prompt" -> "a cat in a box"}]Scope (3)
Text (2)
Chat (2)
Respond to a chat containing multiple messages:
ServiceExecute["xAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "hello there"|>, <|"Role" -> "User", "Content" -> "I'm bored"|>}}]Change the sampling temperature:
ServiceExecute["xAI", "Chat", {"Messages" -> {<|"Role" -> "user", "Content" -> "hello there"|>}, "Temperature" -> 1}]Limit the number of characters returned:
ServiceExecute["xAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "hello there. Tell me something about bears"|>}, "MaxTokens" -> 100}]ServiceExecute["xAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "hello there. Tell me something brief about bears"|>}, "N" -> 2}]Allow the model to use an LLMTool:
tool = LLMTool["countcharacter", "string", StringLength[#string]&]ServiceExecute["xAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "count the characters in this request"|>}, "Tools" -> tool}]ServiceExecute["xAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "hello there. Tell me something about this picture"|>, <|"Role" -> "User", "Content" -> [image]|>}}]Send a chat request asynchronously using ServiceSubmit and collect the response using the HandlerFunctions and HandlerFunctionsKeys options:
res = {};ServiceSubmit["xAI", "Chat", {"Messages" -> {<|"Role" -> "User", "Content" -> "hello there"|>, <|"Role" -> "User", "Content" -> "I'm bored"|>}, "Stream" -> True}, HandlerFunctionsKeys -> "BodyChunkProcessed", HandlerFunctions -> <|"BodyChunkReceived" -> Function[AppendTo[res, #]]|>];
TaskWait[%]
Query["BodyChunkProcessed", All, "ContentChunk"] /@ res//FlattenImage (1)
ImageCreate (1)
Create an Image:
ServiceExecute["xAI", "ImageCreate", {"Prompt" -> "the cutest little cat"}]ServiceExecute["xAI", "ImageCreate", {"Prompt" -> "the cutest little cat", "N" -> 2}]Authentication (4)
If no connections exist, ServiceConnect will prompt a dialog where an API key can be entered:
ServiceConnect["xAI"]The API key can also be specified using the Authentication option:
ServiceConnect["xAI", Authentication -> "myxAIkey"]Use credentials stored in SystemCredential:
SystemCredential["key"] = "myxAIKey";
so = ServiceConnect["xAI", Authentication -> SystemCredential["key"]]The credentials are stored directly by the framework, since SystemCredential["key"] evaluates to a string:
so["Authentication"]Only store the SystemCredential key name rather than its value by using RuleDelayed:
so2 = ServiceConnect["xAI", Authentication :> SystemCredential["key"]];
so2["Authentication"]
%//ReleaseHoldRetrieve the value of the authentication credentials used in a specific service object:
so = ServiceConnect["xAI", Authentication -> "myxAIkey"];
so["Authentication"]Overwrite the authentication credentials of an existing service object:
so = ServiceConnect["xAI", Authentication -> "myxAIkey"];
so["Authentication"]ServiceConnect[so, Authentication -> "otherKey"];
so["Authentication"]See Also
Service Connections: OpenAI ▪ Anthropic ▪ GoogleGemini ▪ Cohere ▪ DeepSeek ▪ Groq ▪ MistralAI ▪ TogetherAI