OllamaHttpClient
public class OllamaHttpClient
io/agentscope/extensions/model/ollama/OllamaHttpClient.java:55–323Ollama HTTP 客户端,封装与远端服务的连接和调用。位于 io.agentscope.extensions.model.ollama 包。核心成员包括 chat、embed、call、stream、getBaseUrl、close。
HTTP client for Ollama API.
This client handles communication with Ollama's chat and embedding APIs. It provides both synchronous and asynchronous (streaming) request capabilities with proper JSON serialization/deserialization and error handling.
Features:
-
Synchronous and streaming request support
-
NDJSON stream parsing for streaming responses
-
Automatic JSON serialization/deserialization with snake_case naming
-
Proper error handling with detailed exception information
-
Configurable base URL and HTTP transport
Default base URL is http://localhost:11434, which is the standard Ollama server address.
嵌套类摘要Nested Class Summary
| 类Class | 类型Kind | 说明Description |
|---|---|---|
Builder | 类class | OllamaHttpClient的构建器。 Builder for OllamaHttpClient. |
OllamaHttpException | 类class | Ollama HTTP 异常,表示执行过程中的错误。 Exception thrown when Ollama HTTP operations fail. |
字段摘要Field Summary
| 字段Field | 类型Type | 说明Description |
|---|---|---|
DEFAULT_BASE_URLstaticfinal | String | 默认基类 URL字段。 Default base URL for Ollama API. |
CHAT_ENDPOINTstaticfinal | String | 聊天端点字段。 Chat API endpoint. |
EMBED_ENDPOINTstaticfinal | String | EMBED 端点字段。 Embed API endpoint. |
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public OllamaHttpClient(HttpTransport transport, String baseUrl) | 创建新的OllamaHttpClient。 Create a new OllamaHttpClient. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public OllamaResponse chat(OllamaRequest request) | 设置聊天。 Make a synchronous Chat API call. |
public OllamaEmbeddingResponse embed(OllamaEmbeddingRequest request) | 设置该参数。 Make a synchronous Embed API call. |
public OllamaResponse call(String endpoint, Object request) | 向智能体发送输入并返回最终回复。 Make a synchronous API call to specific endpoint. |
public T T call(String endpoint, Object request, Class<T> responseType) | 向智能体发送输入并返回最终回复。 Make a synchronous API call to specific endpoint with custom response type. |
public Flux<OllamaResponse> stream(OllamaRequest request) | 设置流。 Make a streaming Chat API call. |
public String getBaseUrl() | 获取基类 URL。 Get the base URL. |
public void close() | 关闭连接并释放相关资源。 Close the transport. |
public static Builder builder()static | 创建用于OllamaHttpClient的构建器。 Create a new builder for OllamaHttpClient. |
字段详情Field Detail
DEFAULT_BASE_URL
public static final String DEFAULT_BASE_URL
默认基类 URL字段。声明为 public static final String DEFAULT_BASE_URL。
Default base URL for Ollama API.
io/agentscope/extensions/model/ollama/OllamaHttpClient.java:60CHAT_ENDPOINT
public static final String CHAT_ENDPOINT
聊天端点字段。声明为 public static final String CHAT_ENDPOINT。
Chat API endpoint.
io/agentscope/extensions/model/ollama/OllamaHttpClient.java:63EMBED_ENDPOINT
public static final String EMBED_ENDPOINT
EMBED 端点字段。声明为 public static final String EMBED_ENDPOINT。
Embed API endpoint.
io/agentscope/extensions/model/ollama/OllamaHttpClient.java:66构造器详情Constructor Detail
OllamaHttpClient
public OllamaHttpClient(HttpTransport transport, String baseUrl)
创建新的OllamaHttpClient。
Create a new OllamaHttpClient.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
transport | HttpTransport | HTTP 传输层到使用。the HTTP transport to use |
baseUrl | String | 基类 URL(类型 `String`)。the base URL (null for default) |
io/agentscope/extensions/model/ollama/OllamaHttpClient.java:77–80方法详情Method Detail
chat
public OllamaResponse chat(OllamaRequest request)
设置聊天。
Make a synchronous Chat API call.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OllamaRequest | 请求(类型 `OllamaRequest`)。the Ollama request |
OllamaResponseio/agentscope/extensions/model/ollama/OllamaHttpClient.java:88–90embed
public OllamaEmbeddingResponse embed(OllamaEmbeddingRequest request)
设置该参数。
Make a synchronous Embed API call.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OllamaEmbeddingRequest | 请求(类型 `OllamaEmbeddingRequest`)。the Embed request |
OllamaEmbeddingResponseio/agentscope/extensions/model/ollama/OllamaHttpClient.java:98–100call
public OllamaResponse call(String endpoint, Object request)
向智能体发送输入并返回最终回复。
Make a synchronous API call to specific endpoint.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
endpoint | String | API 端点。the API endpoint |
request | Object | 请求对象。the request object |
OllamaResponseio/agentscope/extensions/model/ollama/OllamaHttpClient.java:109–111call
public T T call(String endpoint, Object request, Class<T> responseType)
向智能体发送输入并返回最终回复。
Make a synchronous API call to specific endpoint with custom response type.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
endpoint | String | API 端点。the API endpoint |
request | Object | 请求对象。the request object |
responseType | Class<T> | 响应类。the response class |
Tio/agentscope/extensions/model/ollama/OllamaHttpClient.java:122–175stream
public Flux<OllamaResponse> stream(OllamaRequest request)
设置流。
Make a streaming Chat API call.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | OllamaRequest | 请求(类型 `OllamaRequest`)。the Ollama request |
Flux<OllamaResponse>io/agentscope/extensions/model/ollama/OllamaHttpClient.java:183–232getBaseUrl
public String getBaseUrl()
获取基类 URL。
Get the base URL.
Stringio/agentscope/extensions/model/ollama/OllamaHttpClient.java:239–241close
public void close()
关闭连接并释放相关资源。
Close the transport.
io/agentscope/extensions/model/ollama/OllamaHttpClient.java:246–248builder
public static Builder builder()
创建用于OllamaHttpClient的构建器。
Create a new builder for OllamaHttpClient.
Builderio/agentscope/extensions/model/ollama/OllamaHttpClient.java:255–257嵌套类详情Nested Class Detail
Builder
public static class BuilderOllamaHttpClient的构建器。位于 io.agentscope.extensions.model.ollama 包。核心成员包括 transport、baseUrl、build。
Builder for OllamaHttpClient.
OllamaHttpException
public static class OllamaHttpException extends RuntimeExceptionOllama HTTP 异常,表示执行过程中的错误。位于 io.agentscope.extensions.model.ollama 包,继承自 RuntimeException。核心成员包括 getStatusCode、getResponseBody、OllamaHttpException。
Exception thrown when Ollama HTTP operations fail.