23
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 | class Upstage(OpenAI):
"""升级 LLM。
示例:
`pip install llama-index-llms-upstage`
```python
from llama_index.llms.upstage import Upstage
import os
os.environ["UPSTAGE_API_KEY"] = "YOUR_API_KEY"
llm = Upstage()
stream = llm.stream("Hello, how are you?")
for response in stream:
print(response.delta, end="")
```"""
model: str = Field(
default=DEFAULT_UPSTAGE_MODEL, description="The Upstage model to use."
)
temperature: float = Field(
default=DEFAULT_TEMPERATURE,
description="The temperature to use during generation.",
gte=0.0,
lte=1.0,
)
max_tokens: Optional[int] = Field(
description="The maximum number of tokens to generate."
)
logprobs: Optional[bool] = Field(
description="Whether to return logprobs per token."
)
top_logprobs: int = Field(
description="The number of top token logprobs to return.",
default=0,
gte=0,
lte=20,
)
additional_kwargs: Dict[str, Any] = Field(
description="Additional kwargs for the Upstage API.", default_factory=dict
)
max_retries: int = Field(
description="The maximum number of API retries.", default=3, gte=0
)
timeout: float = Field(
description="The timeout, in seconds, for API requests.", default=60.0, gte=0.0
)
reuse_client: bool = Field(
description=(
"Reuse the OpenAI client between requests. When doing anything with large "
"volumes of async API calls, setting this to false can improve stability."
),
default=True,
)
api_key: str = Field(default=None, description="The Upstage API key.")
api_base: str = Field(
default="https://api.upstage.ai/v1/solar",
description="The Upstage API base URL.",
)
_client: Optional[SyncOpenAI] = PrivateAttr()
_aclient: Optional[AsyncOpenAI] = PrivateAttr()
_http_client: Optional[httpx.Client] = PrivateAttr()
def __init__(
self,
model: str = DEFAULT_UPSTAGE_MODEL,
temperature: float = DEFAULT_TEMPERATURE,
max_tokens: Optional[int] = None,
logprobs: Optional[bool] = None,
top_logprobs: int = 0,
additional_kwargs: Dict[str, Any] = None,
max_retries: int = 3,
timeout: float = 60.0,
reuse_client: bool = True,
api_key: Optional[str] = None,
api_base: Optional[str] = None,
callback_manager: Optional[CallbackManager] = None,
default_headers: Optional[Dict[str, str]] = None,
http_client: Optional[httpx.Client] = None, # from base class
system_prompt: Optional[str] = None,
messages_to_prompt: Optional[Callable[[Sequence[ChatMessage]], str]] = None,
completion_to_prompt: Optional[Callable[[str], str]] = None,
pydantic_program_mode: PydanticProgramMode = PydanticProgramMode.DEFAULT,
output_parser: Optional[BaseOutputParser] = None,
**kwargs: Any
) -> None:
additional_kwargs = additional_kwargs or {}
api_key, api_base = resolve_upstage_credentials(
api_key=api_key, api_base=api_base
)
super().__init__(
model=model,
temperature=temperature,
max_tokens=max_tokens,
logprobs=logprobs,
top_logprobs=top_logprobs,
additional_kwargs=additional_kwargs,
max_retries=max_retries,
timeout=timeout,
reuse_client=reuse_client,
api_key=api_key,
api_base=api_base,
callback_manager=callback_manager,
default_headers=default_headers,
http_client=http_client,
system_prompt=system_prompt,
messages_to_prompt=messages_to_prompt,
completion_to_prompt=completion_to_prompt,
pydantic_program_mode=pydantic_program_mode,
output_parser=output_parser,
**kwargs
)
self._client = None
self._aclient = None
self._http_client = http_client
def _get_model_name(self) -> str:
return self.model
@classmethod
def class_name(cls) -> str:
return "upstage_llm"
@property
def metadata(self) -> LLMMetadata:
return LLMMetadata(
context_window=upstage_modelname_to_contextsize(
modelname=self._get_model_name()
),
num_output=self.max_tokens or -1,
is_chat_model=is_chat_model(model=self._get_model_name()),
is_function_calling_model=is_function_calling_model(
model=self._get_model_name()
),
model_name=self.model,
)
|