llama-index-llms-openai 0.2.3__tar.gz → 0.2.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: llama-index-llms-openai
3
- Version: 0.2.3
3
+ Version: 0.2.5
4
4
  Summary: llama-index llms openai integration
5
5
  License: MIT
6
6
  Author: llama-index
@@ -10,6 +10,7 @@ Classifier: Programming Language :: Python :: 3
10
10
  Classifier: Programming Language :: Python :: 3.9
11
11
  Classifier: Programming Language :: Python :: 3.10
12
12
  Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
13
14
  Requires-Dist: llama-index-agent-openai (>=0.3.1,<0.4.0)
14
15
  Requires-Dist: llama-index-core (>=0.11.7,<0.12.0)
15
16
  Requires-Dist: openai (>=1.40.0,<2.0.0)
@@ -52,6 +52,7 @@ from llama_index.core.llms.function_calling import FunctionCallingLLM
52
52
  from llama_index.core.llms.llm import ToolSelection
53
53
  from llama_index.core.types import BaseOutputParser, PydanticProgramMode, Model
54
54
  from llama_index.llms.openai.utils import (
55
+ O1_MODELS,
55
56
  OpenAIToolCall,
56
57
  create_retry_decorator,
57
58
  from_openai_completion_logprobs,
@@ -253,6 +254,10 @@ class OpenAI(FunctionCallingLLM):
253
254
  api_version=api_version,
254
255
  )
255
256
 
257
+ # TODO: Temp forced to 1.0 for o1
258
+ if model in O1_MODELS:
259
+ temperature = 1.0
260
+
256
261
  super().__init__(
257
262
  model=model,
258
263
  temperature=temperature,
@@ -331,6 +336,10 @@ class OpenAI(FunctionCallingLLM):
331
336
  model=self._get_model_name()
332
337
  ),
333
338
  model_name=self.model,
339
+ # TODO: Temp for O1 beta
340
+ system_role=MessageRole.USER
341
+ if self.model in O1_MODELS
342
+ else MessageRole.SYSTEM,
334
343
  )
335
344
 
336
345
  @llm_chat_callback()
@@ -410,7 +419,7 @@ class OpenAI(FunctionCallingLLM):
410
419
  @llm_retry_decorator
411
420
  def _chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
412
421
  client = self._get_client()
413
- message_dicts = to_openai_message_dicts(messages)
422
+ message_dicts = to_openai_message_dicts(messages, model=self.model)
414
423
 
415
424
  if self.reuse_client:
416
425
  response = client.chat.completions.create(
@@ -492,7 +501,7 @@ class OpenAI(FunctionCallingLLM):
492
501
  self, messages: Sequence[ChatMessage], **kwargs: Any
493
502
  ) -> ChatResponseGen:
494
503
  client = self._get_client()
495
- message_dicts = to_openai_message_dicts(messages)
504
+ message_dicts = to_openai_message_dicts(messages, model=self.model)
496
505
 
497
506
  def gen() -> ChatResponseGen:
498
507
  content = ""
@@ -698,7 +707,7 @@ class OpenAI(FunctionCallingLLM):
698
707
  self, messages: Sequence[ChatMessage], **kwargs: Any
699
708
  ) -> ChatResponse:
700
709
  aclient = self._get_aclient()
701
- message_dicts = to_openai_message_dicts(messages)
710
+ message_dicts = to_openai_message_dicts(messages, model=self.model)
702
711
 
703
712
  if self.reuse_client:
704
713
  response = await aclient.chat.completions.create(
@@ -731,7 +740,7 @@ class OpenAI(FunctionCallingLLM):
731
740
  self, messages: Sequence[ChatMessage], **kwargs: Any
732
741
  ) -> ChatResponseAsyncGen:
733
742
  aclient = self._get_aclient()
734
- message_dicts = to_openai_message_dicts(messages)
743
+ message_dicts = to_openai_message_dicts(messages, model=self.model)
735
744
 
736
745
  async def gen() -> ChatResponseAsyncGen:
737
746
  content = ""
@@ -29,6 +29,10 @@ DEFAULT_OPENAI_API_TYPE = "open_ai"
29
29
  DEFAULT_OPENAI_API_BASE = "https://api.openai.com/v1"
30
30
  DEFAULT_OPENAI_API_VERSION = ""
31
31
 
32
+ O1_MODELS: Dict[str, int] = {
33
+ "o1-preview": 128000,
34
+ "o1-mini": 128000,
35
+ }
32
36
 
33
37
  GPT4_MODELS: Dict[str, int] = {
34
38
  # stable model names:
@@ -109,6 +113,7 @@ GPT3_MODELS: Dict[str, int] = {
109
113
  }
110
114
 
111
115
  ALL_AVAILABLE_MODELS = {
116
+ **O1_MODELS,
112
117
  **GPT4_MODELS,
113
118
  **TURBO_MODELS,
114
119
  **GPT3_5_MODELS,
@@ -117,6 +122,7 @@ ALL_AVAILABLE_MODELS = {
117
122
  }
118
123
 
119
124
  CHAT_MODELS = {
125
+ **O1_MODELS,
120
126
  **GPT4_MODELS,
121
127
  **TURBO_MODELS,
122
128
  **AZURE_TURBO_MODELS,
@@ -220,11 +226,15 @@ def is_chat_model(model: str) -> bool:
220
226
  def is_function_calling_model(model: str) -> bool:
221
227
  is_chat_model_ = is_chat_model(model)
222
228
  is_old = "0314" in model or "0301" in model
223
- return is_chat_model_ and not is_old
229
+
230
+ # TODO: This is temporary for openai's beta
231
+ is_o1_beta = "o1" in model
232
+
233
+ return is_chat_model_ and not is_old and not is_o1_beta
224
234
 
225
235
 
226
236
  def to_openai_message_dict(
227
- message: ChatMessage, drop_none: bool = False
237
+ message: ChatMessage, drop_none: bool = False, model: Optional[str] = None
228
238
  ) -> ChatCompletionMessageParam:
229
239
  """Convert generic message to OpenAI message dict."""
230
240
  message_dict = {
@@ -232,6 +242,11 @@ def to_openai_message_dict(
232
242
  "content": message.content,
233
243
  }
234
244
 
245
+ # TODO: O1 models do not support system prompts
246
+ if model is not None and model in O1_MODELS:
247
+ if message_dict["role"] == "system":
248
+ message_dict["role"] = "user"
249
+
235
250
  # NOTE: openai messages have additional arguments:
236
251
  # - function messages have `name`
237
252
  # - assistant messages have optional `function_call`
@@ -247,11 +262,14 @@ def to_openai_message_dict(
247
262
 
248
263
 
249
264
  def to_openai_message_dicts(
250
- messages: Sequence[ChatMessage], drop_none: bool = False
265
+ messages: Sequence[ChatMessage],
266
+ drop_none: bool = False,
267
+ model: Optional[str] = None,
251
268
  ) -> List[ChatCompletionMessageParam]:
252
269
  """Convert generic messages to OpenAI message dicts."""
253
270
  return [
254
- to_openai_message_dict(message, drop_none=drop_none) for message in messages
271
+ to_openai_message_dict(message, drop_none=drop_none, model=model)
272
+ for message in messages
255
273
  ]
256
274
 
257
275
 
@@ -29,7 +29,7 @@ exclude = ["**/BUILD"]
29
29
  license = "MIT"
30
30
  name = "llama-index-llms-openai"
31
31
  readme = "README.md"
32
- version = "0.2.3"
32
+ version = "0.2.5"
33
33
 
34
34
  [tool.poetry.dependencies]
35
35
  python = ">=3.8.1,<4.0"