类 OpenAI
OpenAILike #
基类: OpenAI
OpenAILike LLM。
OpenAILike 是对 OpenAI 模型的轻量级封装,使其兼容提供 OpenAI 兼容 API 的第三方工具。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
model
|
str
|
用于 API 的模型。 |
DEFAULT_OPENAI_MODEL
|
api_base
|
str
|
用于 API 的基本 URL。默认为 "https://api.openai.com/v1"。 |
无
|
is_chat_model
|
bool
|
模型是否使用聊天或补全端点。默认为 False。 |
必需 |
is_function_calling_model
|
bool
|
模型是否支持通过 API 调用 OpenAI 函数/工具。默认为 False。 |
必需 |
api_key
|
str
|
用于 API 的 API 密钥。如果您的 API 不需要 API 密钥,请将其设置为随机字符串。 |
无
|
context_window
|
int
|
用于 API 的上下文窗口。为获得最佳体验,请将其设置为您模型的上下文窗口。默认为 3900。 |
必需 |
max_tokens
|
int
|
要生成的最大 Token 数。默认为 None。 |
无
|
temperature
|
float
|
用于 API 的温度。默认为 0.1。 |
DEFAULT_TEMPERATURE
|
additional_kwargs
|
dict
|
指定请求体中的附加参数。 |
无
|
max_retries
|
int
|
API 调用失败时重试的次数。默认为 3。 |
3
|
timeout
|
float
|
在 API 调用失败前等待的秒数。默认为 60.0。 |
60.0
|
reuse_client
|
bool
|
在请求之间复用 OpenAI 客户端。默认为 True。 |
True
|
default_headers
|
dict
|
覆盖 API 请求的默认头部。默认为 None。 |
无
|
http_client
|
Client
|
传入您自己的 httpx.Client 实例。默认为 None。 |
无
|
async_http_client
|
AsyncClient
|
传入您自己的 httpx.AsyncClient 实例。默认为 None。 |
无
|
示例
pip install llama-index-llms-openai-like
from llama_index.llms.openai_like import OpenAILike
llm = OpenAILike(
model="my model",
api_base="https://hostname.com/v1",
api_key="fake",
context_window=128000,
is_chat_model=True,
is_function_calling_model=False,
)
response = llm.complete("Hello World!")
print(str(response))
源代码位于 llama-index-integrations/llms/llama-index-llms-openai-like/llama_index/llms/openai_like/base.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
complete #
complete(prompt: str, formatted: bool = False, **kwargs: Any) -> CompletionResponse
补全 Prompt。
源代码位于 llama-index-integrations/llms/llama-index-llms-openai-like/llama_index/llms/openai_like/base.py
138 139 140 141 142 143 144 145 |
|
stream_complete #
stream_complete(prompt: str, formatted: bool = False, **kwargs: Any) -> CompletionResponseGen
流式补全 Prompt。
源代码位于 llama-index-integrations/llms/llama-index-llms-openai-like/llama_index/llms/openai_like/base.py
147 148 149 150 151 152 153 154 |
|
chat #
chat(messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse
与模型聊天。
源代码位于 llama-index-integrations/llms/llama-index-llms-openai-like/llama_index/llms/openai_like/base.py
156 157 158 159 160 161 162 163 |
|
acomplete async
#
acomplete(prompt: str, formatted: bool = False, **kwargs: Any) -> CompletionResponse
补全 Prompt。
源代码位于 llama-index-integrations/llms/llama-index-llms-openai-like/llama_index/llms/openai_like/base.py
177 178 179 180 181 182 183 184 |
|
astream_complete async
#
astream_complete(prompt: str, formatted: bool = False, **kwargs: Any) -> CompletionResponseAsyncGen
流式补全 Prompt。
源代码位于 llama-index-integrations/llms/llama-index-llms-openai-like/llama_index/llms/openai_like/base.py
186 187 188 189 190 191 192 193 |
|
achat async
#
achat(messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse
与模型聊天。
源代码位于 llama-index-integrations/llms/llama-index-llms-openai-like/llama_index/llms/openai_like/base.py
195 196 197 198 199 200 201 202 203 204 |
|