pub struct LanguageModelRequest<M>where
M: LanguageModel,{
pub model: M,
pub prompt: Option<String>,
/* private fields */
}Expand description
Options for text generation requests such as generate_text and stream_text.
Fields§
§model: MThe language model to use for text generation.
prompt: Option<String>An optional simple text prompt for the request.
This should not be set if messages are provided in the options.
Implementations§
Source§impl<M> LanguageModelRequest<M>where
M: LanguageModel,
impl<M> LanguageModelRequest<M>where
M: LanguageModel,
Sourcepub async fn generate_text(&mut self) -> Result<GenerateTextResponse, Error>
pub async fn generate_text(&mut self) -> Result<GenerateTextResponse, Error>
Generates text and executes tools using the language model.
This method performs non-streaming text generation, potentially involving multiple steps of tool calling and execution until the conversation reaches a natural stopping point. The model may call tools based on the configured options, and responses are processed iteratively until completion.
For streaming responses, use stream_text instead.
§Returns
A GenerateTextResponse containing the final conversation state and generated content.
§Errors
Returns an Error if the underlying language model fails to generate a response
or if tool execution encounters an error.
§Examples
use aisdk::{
core::{LanguageModelRequest},
providers::OpenAI,
};
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let openai = OpenAI::gpt_5();
let result = LanguageModelRequest::builder()
.model(openai)
.prompt("What is the meaning of life?")
.build()
.generate_text()
.await?;
println!("{}", result.text().unwrap());
Ok(())
}Source§impl<M> LanguageModelRequest<M>where
M: LanguageModel,
impl<M> LanguageModelRequest<M>where
M: LanguageModel,
Sourcepub fn builder() -> LanguageModelRequestBuilder<M>
pub fn builder() -> LanguageModelRequestBuilder<M>
Creates a new builder for constructing a LanguageModelRequest.
This method initiates the type-state builder pattern, starting with the
ModelStage where you must specify the language model.
Source§impl<M> LanguageModelRequest<M>where
M: LanguageModel,
impl<M> LanguageModelRequest<M>where
M: LanguageModel,
Sourcepub async fn stream_text(&mut self) -> Result<StreamTextResponse, Error>
pub async fn stream_text(&mut self) -> Result<StreamTextResponse, Error>
Streams text generation and tool execution using the language model.
This method performs streaming text generation, providing real-time access to response chunks as they are produced. It supports tool calling and execution in multiple steps, streaming intermediate results and handling tool interactions dynamically.
For non-streaming responses, use generate_text instead.
§Returns
A StreamTextResponse containing the stream of chunks and final conversation state.
§Errors
Returns an Error if the underlying language model fails to generate a response
or if tool execution encounters an error.
§Examples
use aisdk::{
core::{LanguageModelRequest, LanguageModelStreamChunkType},
providers::OpenAI,
};
use futures::StreamExt;
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let openai = OpenAI::gpt_5();
let mut stream = LanguageModelRequest::builder()
.model(openai)
.prompt("What is the meaning of life?")
.build()
.stream_text()
.await?
.stream;
while let Some(chunk) = stream.next().await {
if let LanguageModelStreamChunkType::Text(text) = chunk {
println!("{}", text);
}
}
Ok(())
}Methods from Deref<Target = LanguageModelOptions>§
Sourcepub fn step(&self, index: usize) -> Option<Step>
pub fn step(&self, index: usize) -> Option<Step>
Returns the step with the given index, if it exists.
Sourcepub fn content(&self) -> Option<&LanguageModelResponseContentType>
pub fn content(&self) -> Option<&LanguageModelResponseContentType>
Returns the content of the last assistant message, excluding reasoning.
Sourcepub fn tool_results(&self) -> Option<Vec<ToolResultInfo>>
pub fn tool_results(&self) -> Option<Vec<ToolResultInfo>>
Extracts all tool results from the conversation.
Sourcepub fn tool_calls(&self) -> Option<Vec<ToolCallInfo>>
pub fn tool_calls(&self) -> Option<Vec<ToolCallInfo>>
Extracts all tool calls from the conversation.
Sourcepub fn stop_reason(&self) -> Option<StopReason>
pub fn stop_reason(&self) -> Option<StopReason>
Returns the reason why generation stopped.