AS AgentScopeAPI 手册 / API Reference

OllamaHttpClient

public class OllamaHttpClient
源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:55–323

Ollama 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_URL
staticfinal
String
默认基类 URL字段。
Default base URL for Ollama API.
CHAT_ENDPOINT
staticfinal
String
聊天端点字段。
Chat API endpoint.
EMBED_ENDPOINT
staticfinal
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
staticfinal

默认基类 URL字段。声明为 public static final String DEFAULT_BASE_URL。

Default base URL for Ollama API.

源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:60

CHAT_ENDPOINT

public static final String CHAT_ENDPOINT
staticfinal

聊天端点字段。声明为 public static final String CHAT_ENDPOINT。

Chat API endpoint.

源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:63

EMBED_ENDPOINT

public static final String EMBED_ENDPOINT
staticfinal

EMBED 端点字段。声明为 public static final String EMBED_ENDPOINT。

Embed API endpoint.

源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:66

构造器详情Constructor Detail

OllamaHttpClient

public OllamaHttpClient(HttpTransport transport, String baseUrl)

创建新的OllamaHttpClient。

Create a new OllamaHttpClient.

参数Parameters
名称Name类型Type说明Description
transportHttpTransportHTTP 传输层到使用。the HTTP transport to use
baseUrlString基类 URL(类型 `String`)。the base URL (null for default)
源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:77–80

方法详情Method Detail

chat

public OllamaResponse chat(OllamaRequest request)

设置聊天。

Make a synchronous Chat API call.

参数Parameters
名称Name类型Type说明Description
requestOllamaRequest请求(类型 `OllamaRequest`)。the Ollama request
返回Returns OllamaResponse
返回OllamaResponse。the Ollama response
源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:88–90

embed

public OllamaEmbeddingResponse embed(OllamaEmbeddingRequest request)

设置该参数。

Make a synchronous Embed API call.

参数Parameters
名称Name类型Type说明Description
requestOllamaEmbeddingRequest请求(类型 `OllamaEmbeddingRequest`)。the Embed request
返回Returns OllamaEmbeddingResponse
返回OllamaEmbeddingResponse。the Embed response
源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:98–100

call

public OllamaResponse call(String endpoint, Object request)

向智能体发送输入并返回最终回复。

Make a synchronous API call to specific endpoint.

参数Parameters
名称Name类型Type说明Description
endpointStringAPI 端点。the API endpoint
requestObject请求对象。the request object
返回Returns OllamaResponse
返回OllamaResponse。the Ollama response
源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:109–111

call

public T T call(String endpoint, Object request, Class<T> responseType)

向智能体发送输入并返回最终回复。

Make a synchronous API call to specific endpoint with custom response type.

参数Parameters
名称Name类型Type说明Description
endpointStringAPI 端点。the API endpoint
requestObject请求对象。the request object
responseTypeClass<T>响应类。the response class
返回Returns T
响应对象。the response object
源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:122–175

stream

public Flux<OllamaResponse> stream(OllamaRequest request)

设置流。

Make a streaming Chat API call.

参数Parameters
名称Name类型Type说明Description
requestOllamaRequest请求(类型 `OllamaRequest`)。the Ollama request
返回Returns Flux<OllamaResponse>
返回Flux。a Flux of Ollama responses
源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:183–232

getBaseUrl

public String getBaseUrl()

获取基类 URL。

Get the base URL.

返回Returns String
基类 URL。the base URL
源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:239–241

close

public void close()

关闭连接并释放相关资源。

Close the transport.

源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:246–248

builder

public static Builder builder()
static

创建用于OllamaHttpClient的构建器。

Create a new builder for OllamaHttpClient.

返回Returns Builder
一种新的构建器实例。a new Builder instance
源码Source io/agentscope/extensions/model/ollama/OllamaHttpClient.java:255–257

嵌套类详情Nested Class Detail

Builder

public static class Builder

OllamaHttpClient的构建器。位于 io.agentscope.extensions.model.ollama 包。核心成员包括 transport、baseUrl、build。

Builder for OllamaHttpClient.

OllamaHttpException

public static class OllamaHttpException extends RuntimeException

Ollama HTTP 异常,表示执行过程中的错误。位于 io.agentscope.extensions.model.ollama 包,继承自 RuntimeException。核心成员包括 getStatusCode、getResponseBody、OllamaHttpException。

Exception thrown when Ollama HTTP operations fail.

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.