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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942 | class OpenAI(FunctionCallingLLM):
"""
OpenAI LLM.
Args:
model: name of the OpenAI model to use.
temperature: a float from 0 to 1 controlling randomness in generation; higher will lead to more creative, less deterministic responses.
max_tokens: the maximum number of tokens to generate.
additional_kwargs: Add additional parameters to OpenAI request body.
max_retries: How many times to retry the API call if it fails.
timeout: How long to wait, in seconds, for an API call before failing.
reuse_client: Reuse the OpenAI client between requests. When doing anything with large volumes of async API calls, setting this to false can improve stability.
api_key: Your OpenAI api key
api_base: The base URL of the API to call
api_version: the version of the API to call
callback_manager: the callback manager is used for observability.
default_headers: override the default headers for API requests.
http_client: pass in your own httpx.Client instance.
async_http_client: pass in your own httpx.AsyncClient instance.
Examples:
`pip install llama-index-llms-openai`
```python
import os
import openai
os.environ["OPENAI_API_KEY"] = "sk-..."
openai.api_key = os.environ["OPENAI_API_KEY"]
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-3.5-turbo")
stream = llm.stream("Hi, write a short story")
for r in stream:
print(r.delta, end="")
```
"""
model: str = Field(
default=DEFAULT_OPENAI_MODEL, description="The OpenAI 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.",
gt=0,
)
logprobs: Optional[bool] = Field(
description="Whether to return logprobs per token.",
default=None,
)
top_logprobs: int = Field(
description="The number of top token log probs to return.",
default=0,
gte=0,
lte=20,
)
additional_kwargs: Dict[str, Any] = Field(
default_factory=dict, description="Additional kwargs for the OpenAI API."
)
max_retries: int = Field(
default=3,
description="The maximum number of API retries.",
gte=0,
)
timeout: float = Field(
default=60.0,
description="The timeout, in seconds, for API requests.",
gte=0,
)
default_headers: Optional[Dict[str, str]] = Field(
default=None, description="The default headers for API requests."
)
reuse_client: bool = Field(
default=True,
description=(
"Reuse the OpenAI client between requests. When doing anything with large "
"volumes of async API calls, setting this to false can improve stability."
),
)
api_key: str = Field(default=None, description="The OpenAI API key.")
api_base: str = Field(description="The base URL for OpenAI API.")
api_version: str = Field(description="The API version for OpenAI API.")
strict: bool = Field(
default=False,
description="Whether to use strict mode for invoking tools/using schemas.",
)
_client: Optional[SyncOpenAI] = PrivateAttr()
_aclient: Optional[AsyncOpenAI] = PrivateAttr()
_http_client: Optional[httpx.Client] = PrivateAttr()
_async_http_client: Optional[httpx.AsyncClient] = PrivateAttr()
def __init__(
self,
model: str = DEFAULT_OPENAI_MODEL,
temperature: float = DEFAULT_TEMPERATURE,
max_tokens: Optional[int] = None,
additional_kwargs: Optional[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,
api_version: Optional[str] = None,
callback_manager: Optional[CallbackManager] = None,
default_headers: Optional[Dict[str, str]] = None,
http_client: Optional[httpx.Client] = None,
async_http_client: Optional[httpx.AsyncClient] = None,
# 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,
strict: bool = False,
**kwargs: Any,
) -> None:
additional_kwargs = additional_kwargs or {}
api_key, api_base, api_version = resolve_openai_credentials(
api_key=api_key,
api_base=api_base,
api_version=api_version,
)
super().__init__(
model=model,
temperature=temperature,
max_tokens=max_tokens,
additional_kwargs=additional_kwargs,
max_retries=max_retries,
callback_manager=callback_manager,
api_key=api_key,
api_version=api_version,
api_base=api_base,
timeout=timeout,
reuse_client=reuse_client,
default_headers=default_headers,
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,
strict=strict,
**kwargs,
)
self._client = None
self._aclient = None
self._http_client = http_client
self._async_http_client = async_http_client
def _get_client(self) -> SyncOpenAI:
if not self.reuse_client:
return SyncOpenAI(**self._get_credential_kwargs())
if self._client is None:
self._client = SyncOpenAI(**self._get_credential_kwargs())
return self._client
def _get_aclient(self) -> AsyncOpenAI:
if not self.reuse_client:
return AsyncOpenAI(**self._get_credential_kwargs(is_async=True))
if self._aclient is None:
self._aclient = AsyncOpenAI(**self._get_credential_kwargs(is_async=True))
return self._aclient
def _get_model_name(self) -> str:
model_name = self.model
if "ft-" in model_name: # legacy fine-tuning
model_name = model_name.split(":")[0]
elif model_name.startswith("ft:"):
model_name = model_name.split(":")[1]
return model_name
def _is_azure_client(self) -> bool:
return isinstance(self._get_client(), AzureOpenAI)
@classmethod
def class_name(cls) -> str:
return "openai_llm"
@property
def _tokenizer(self) -> Optional[Tokenizer]:
"""
Get a tokenizer for this model, or None if a tokenizing method is unknown.
OpenAI can do this using the tiktoken package, subclasses may not have
this convenience.
"""
return tiktoken.encoding_for_model(self._get_model_name())
@property
def metadata(self) -> LLMMetadata:
return LLMMetadata(
context_window=openai_modelname_to_contextsize(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,
)
@llm_chat_callback()
def chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
if self._use_chat_completions(kwargs):
chat_fn = self._chat
else:
chat_fn = completion_to_chat_decorator(self._complete)
return chat_fn(messages, **kwargs)
@llm_chat_callback()
def stream_chat(
self, messages: Sequence[ChatMessage], **kwargs: Any
) -> ChatResponseGen:
if self._use_chat_completions(kwargs):
stream_chat_fn = self._stream_chat
else:
stream_chat_fn = stream_completion_to_chat_decorator(self._stream_complete)
return stream_chat_fn(messages, **kwargs)
@llm_completion_callback()
def complete(
self, prompt: str, formatted: bool = False, **kwargs: Any
) -> CompletionResponse:
if self._use_chat_completions(kwargs):
complete_fn = chat_to_completion_decorator(self._chat)
else:
complete_fn = self._complete
return complete_fn(prompt, **kwargs)
@llm_completion_callback()
def stream_complete(
self, prompt: str, formatted: bool = False, **kwargs: Any
) -> CompletionResponseGen:
if self._use_chat_completions(kwargs):
stream_complete_fn = stream_chat_to_completion_decorator(self._stream_chat)
else:
stream_complete_fn = self._stream_complete
return stream_complete_fn(prompt, **kwargs)
def _use_chat_completions(self, kwargs: Dict[str, Any]) -> bool:
if "use_chat_completions" in kwargs:
return kwargs["use_chat_completions"]
return self.metadata.is_chat_model
def _get_credential_kwargs(self, is_async: bool = False) -> Dict[str, Any]:
return {
"api_key": self.api_key,
"base_url": self.api_base,
"max_retries": self.max_retries,
"timeout": self.timeout,
"default_headers": self.default_headers,
"http_client": self._async_http_client if is_async else self._http_client,
}
def _get_model_kwargs(self, **kwargs: Any) -> Dict[str, Any]:
base_kwargs = {"model": self.model, "temperature": self.temperature, **kwargs}
if self.max_tokens is not None:
# If max_tokens is None, don't include in the payload:
# https://platform.openai.com/docs/api-reference/chat
# https://platform.openai.com/docs/api-reference/completions
base_kwargs["max_tokens"] = self.max_tokens
if self.logprobs is not None and self.logprobs is True:
if self.metadata.is_chat_model:
base_kwargs["logprobs"] = self.logprobs
base_kwargs["top_logprobs"] = self.top_logprobs
else:
base_kwargs["logprobs"] = self.top_logprobs # int in this case
# can't send stream_options to the API when not streaming
all_kwargs = {**base_kwargs, **self.additional_kwargs}
if "stream" not in all_kwargs and "stream_options" in all_kwargs:
del all_kwargs["stream_options"]
return all_kwargs
@llm_retry_decorator
def _chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
client = self._get_client()
message_dicts = to_openai_message_dicts(messages)
if self.reuse_client:
response = client.chat.completions.create(
messages=message_dicts,
stream=False,
**self._get_model_kwargs(**kwargs),
)
else:
with client:
response = client.chat.completions.create(
messages=message_dicts,
stream=False,
**self._get_model_kwargs(**kwargs),
)
openai_message = response.choices[0].message
message = from_openai_message(openai_message)
openai_token_logprobs = response.choices[0].logprobs
logprobs = None
if openai_token_logprobs and openai_token_logprobs.content:
logprobs = from_openai_token_logprobs(openai_token_logprobs.content)
return ChatResponse(
message=message,
raw=response,
logprobs=logprobs,
additional_kwargs=self._get_response_token_counts(response),
)
def _update_tool_calls(
self,
tool_calls: List[ChoiceDeltaToolCall],
tool_calls_delta: Optional[List[ChoiceDeltaToolCall]],
) -> List[ChoiceDeltaToolCall]:
"""
Use the tool_calls_delta objects received from openai stream chunks
to update the running tool_calls object.
Args:
tool_calls (List[ChoiceDeltaToolCall]): the list of tool calls
tool_calls_delta (ChoiceDeltaToolCall): the delta to update tool_calls
Returns:
List[ChoiceDeltaToolCall]: the updated tool calls
"""
# openai provides chunks consisting of tool_call deltas one tool at a time
if tool_calls_delta is None:
return tool_calls
tc_delta = tool_calls_delta[0]
if len(tool_calls) == 0:
tool_calls.append(tc_delta)
else:
# we need to either update latest tool_call or start a
# new tool_call (i.e., multiple tools in this turn) and
# accumulate that new tool_call with future delta chunks
t = tool_calls[-1]
if t.index != tc_delta.index:
# the start of a new tool call, so append to our running tool_calls list
tool_calls.append(tc_delta)
else:
# not the start of a new tool call, so update last item of tool_calls
# validations to get passed by mypy
assert t.function is not None
assert tc_delta.function is not None
assert t.function.arguments is not None
assert t.function.name is not None
assert t.id is not None
t.function.arguments += tc_delta.function.arguments or ""
t.function.name += tc_delta.function.name or ""
t.id += tc_delta.id or ""
return tool_calls
@llm_retry_decorator
def _stream_chat(
self, messages: Sequence[ChatMessage], **kwargs: Any
) -> ChatResponseGen:
client = self._get_client()
message_dicts = to_openai_message_dicts(messages)
def gen() -> ChatResponseGen:
content = ""
tool_calls: List[ChoiceDeltaToolCall] = []
is_function = False
for response in client.chat.completions.create(
messages=message_dicts,
**self._get_model_kwargs(stream=True, **kwargs),
):
response = cast(ChatCompletionChunk, response)
if len(response.choices) > 0:
delta = response.choices[0].delta
else:
if self._is_azure_client():
continue
else:
delta = ChoiceDelta()
# check if this chunk is the start of a function call
if delta.tool_calls:
is_function = True
# update using deltas
role = delta.role or MessageRole.ASSISTANT
content_delta = delta.content or ""
content += content_delta
additional_kwargs = {}
if is_function:
tool_calls = self._update_tool_calls(tool_calls, delta.tool_calls)
additional_kwargs["tool_calls"] = tool_calls
yield ChatResponse(
message=ChatMessage(
role=role,
content=content,
additional_kwargs=additional_kwargs,
),
delta=content_delta,
raw=response,
additional_kwargs=self._get_response_token_counts(response),
)
return gen()
@llm_retry_decorator
def _complete(self, prompt: str, **kwargs: Any) -> CompletionResponse:
client = self._get_client()
all_kwargs = self._get_model_kwargs(**kwargs)
self._update_max_tokens(all_kwargs, prompt)
if self.reuse_client:
response = client.completions.create(
prompt=prompt,
stream=False,
**all_kwargs,
)
else:
with client:
response = client.completions.create(
prompt=prompt,
stream=False,
**all_kwargs,
)
text = response.choices[0].text
openai_completion_logprobs = response.choices[0].logprobs
logprobs = None
if openai_completion_logprobs:
logprobs = from_openai_completion_logprobs(openai_completion_logprobs)
return CompletionResponse(
text=text,
raw=response,
logprobs=logprobs,
additional_kwargs=self._get_response_token_counts(response),
)
@llm_retry_decorator
def _stream_complete(self, prompt: str, **kwargs: Any) -> CompletionResponseGen:
client = self._get_client()
all_kwargs = self._get_model_kwargs(stream=True, **kwargs)
self._update_max_tokens(all_kwargs, prompt)
def gen() -> CompletionResponseGen:
text = ""
for response in client.completions.create(
prompt=prompt,
**all_kwargs,
):
if len(response.choices) > 0:
delta = response.choices[0].text
if delta is None:
delta = ""
else:
delta = ""
text += delta
yield CompletionResponse(
delta=delta,
text=text,
raw=response,
additional_kwargs=self._get_response_token_counts(response),
)
return gen()
def _update_max_tokens(self, all_kwargs: Dict[str, Any], prompt: str) -> None:
"""Infer max_tokens for the payload, if possible."""
if self.max_tokens is not None or self._tokenizer is None:
return
# NOTE: non-chat completion endpoint requires max_tokens to be set
num_tokens = len(self._tokenizer.encode(prompt))
max_tokens = self.metadata.context_window - num_tokens
if max_tokens <= 0:
raise ValueError(
f"The prompt has {num_tokens} tokens, which is too long for"
" the model. Please use a prompt that fits within"
f" {self.metadata.context_window} tokens."
)
all_kwargs["max_tokens"] = max_tokens
def _get_response_token_counts(self, raw_response: Any) -> dict:
"""Get the token usage reported by the response."""
if hasattr(raw_response, "usage"):
try:
prompt_tokens = raw_response.usage.prompt_tokens
completion_tokens = raw_response.usage.completion_tokens
total_tokens = raw_response.usage.total_tokens
except AttributeError:
return {}
elif isinstance(raw_response, dict):
usage = raw_response.get("usage", {})
# NOTE: other model providers that use the OpenAI client may not report usage
if usage is None:
return {}
# Backwards compatibility with old dict type
prompt_tokens = usage.get("prompt_tokens", 0)
completion_tokens = usage.get("completion_tokens", 0)
total_tokens = usage.get("total_tokens", 0)
else:
return {}
return {
"prompt_tokens": prompt_tokens,
"completion_tokens": completion_tokens,
"total_tokens": total_tokens,
}
# ===== Async Endpoints =====
@llm_chat_callback()
async def achat(
self,
messages: Sequence[ChatMessage],
**kwargs: Any,
) -> ChatResponse:
achat_fn: Callable[..., Awaitable[ChatResponse]]
if self._use_chat_completions(kwargs):
achat_fn = self._achat
else:
achat_fn = acompletion_to_chat_decorator(self._acomplete)
return await achat_fn(messages, **kwargs)
@llm_chat_callback()
async def astream_chat(
self,
messages: Sequence[ChatMessage],
**kwargs: Any,
) -> ChatResponseAsyncGen:
astream_chat_fn: Callable[..., Awaitable[ChatResponseAsyncGen]]
if self._use_chat_completions(kwargs):
astream_chat_fn = self._astream_chat
else:
astream_chat_fn = astream_completion_to_chat_decorator(
self._astream_complete
)
return await astream_chat_fn(messages, **kwargs)
@llm_completion_callback()
async def acomplete(
self, prompt: str, formatted: bool = False, **kwargs: Any
) -> CompletionResponse:
if self._use_chat_completions(kwargs):
acomplete_fn = achat_to_completion_decorator(self._achat)
else:
acomplete_fn = self._acomplete
return await acomplete_fn(prompt, **kwargs)
@llm_completion_callback()
async def astream_complete(
self, prompt: str, formatted: bool = False, **kwargs: Any
) -> CompletionResponseAsyncGen:
if self._use_chat_completions(kwargs):
astream_complete_fn = astream_chat_to_completion_decorator(
self._astream_chat
)
else:
astream_complete_fn = self._astream_complete
return await astream_complete_fn(prompt, **kwargs)
@llm_retry_decorator
async def _achat(
self, messages: Sequence[ChatMessage], **kwargs: Any
) -> ChatResponse:
aclient = self._get_aclient()
message_dicts = to_openai_message_dicts(messages)
if self.reuse_client:
response = await aclient.chat.completions.create(
messages=message_dicts, stream=False, **self._get_model_kwargs(**kwargs)
)
else:
async with aclient:
response = await aclient.chat.completions.create(
messages=message_dicts,
stream=False,
**self._get_model_kwargs(**kwargs),
)
openai_message = response.choices[0].message
message = from_openai_message(openai_message)
openai_token_logprobs = response.choices[0].logprobs
logprobs = None
if openai_token_logprobs and openai_token_logprobs.content:
logprobs = from_openai_token_logprobs(openai_token_logprobs.content)
return ChatResponse(
message=message,
raw=response,
logprobs=logprobs,
additional_kwargs=self._get_response_token_counts(response),
)
@llm_retry_decorator
async def _astream_chat(
self, messages: Sequence[ChatMessage], **kwargs: Any
) -> ChatResponseAsyncGen:
aclient = self._get_aclient()
message_dicts = to_openai_message_dicts(messages)
async def gen() -> ChatResponseAsyncGen:
content = ""
tool_calls: List[ChoiceDeltaToolCall] = []
is_function = False
first_chat_chunk = True
async for response in await aclient.chat.completions.create(
messages=message_dicts,
**self._get_model_kwargs(stream=True, **kwargs),
):
response = cast(ChatCompletionChunk, response)
if len(response.choices) > 0:
# check if the first chunk has neither content nor tool_calls
# this happens when 1106 models end up calling multiple tools
if (
first_chat_chunk
and response.choices[0].delta.content is None
and response.choices[0].delta.tool_calls is None
):
first_chat_chunk = False
continue
delta = response.choices[0].delta
else:
if self._is_azure_client():
continue
else:
delta = ChoiceDelta()
first_chat_chunk = False
# check if this chunk is the start of a function call
if delta.tool_calls:
is_function = True
# update using deltas
role = delta.role or MessageRole.ASSISTANT
content_delta = delta.content or ""
content += content_delta
additional_kwargs = {}
if is_function:
tool_calls = self._update_tool_calls(tool_calls, delta.tool_calls)
additional_kwargs["tool_calls"] = tool_calls
yield ChatResponse(
message=ChatMessage(
role=role,
content=content,
additional_kwargs=additional_kwargs,
),
delta=content_delta,
raw=response,
additional_kwargs=self._get_response_token_counts(response),
)
return gen()
@llm_retry_decorator
async def _acomplete(self, prompt: str, **kwargs: Any) -> CompletionResponse:
aclient = self._get_aclient()
all_kwargs = self._get_model_kwargs(**kwargs)
self._update_max_tokens(all_kwargs, prompt)
if self.reuse_client:
response = await aclient.completions.create(
prompt=prompt,
stream=False,
**all_kwargs,
)
else:
async with aclient:
response = await aclient.completions.create(
prompt=prompt,
stream=False,
**all_kwargs,
)
text = response.choices[0].text
openai_completion_logprobs = response.choices[0].logprobs
logprobs = None
if openai_completion_logprobs:
logprobs = from_openai_completion_logprobs(openai_completion_logprobs)
return CompletionResponse(
text=text,
raw=response,
logprobs=logprobs,
additional_kwargs=self._get_response_token_counts(response),
)
@llm_retry_decorator
async def _astream_complete(
self, prompt: str, **kwargs: Any
) -> CompletionResponseAsyncGen:
aclient = self._get_aclient()
all_kwargs = self._get_model_kwargs(stream=True, **kwargs)
self._update_max_tokens(all_kwargs, prompt)
async def gen() -> CompletionResponseAsyncGen:
text = ""
async for response in await aclient.completions.create(
prompt=prompt,
**all_kwargs,
):
if len(response.choices) > 0:
delta = response.choices[0].text
if delta is None:
delta = ""
else:
delta = ""
text += delta
yield CompletionResponse(
delta=delta,
text=text,
raw=response,
additional_kwargs=self._get_response_token_counts(response),
)
return gen()
def _prepare_chat_with_tools(
self,
tools: List["BaseTool"],
user_msg: Optional[Union[str, ChatMessage]] = None,
chat_history: Optional[List[ChatMessage]] = None,
verbose: bool = False,
allow_parallel_tool_calls: bool = False,
tool_choice: Union[str, dict] = "auto",
strict: Optional[bool] = None,
**kwargs: Any,
) -> Dict[str, Any]:
"""Predict and call the tool."""
from llama_index.agent.openai.utils import resolve_tool_choice
# misralai uses the same openai tool format
tool_specs = [tool.metadata.to_openai_tool() for tool in tools]
# if strict is passed in, use, else default to the class-level attribute, else default to True`
if strict is not None:
strict = strict
else:
strict = self.strict
if self.metadata.is_function_calling_model:
for tool_spec in tool_specs:
if tool_spec["type"] == "function":
tool_spec["function"]["strict"] = strict
tool_spec["function"]["parameters"][
"additionalProperties"
] = False # in current openai 1.40.0 it is always false.
if isinstance(user_msg, str):
user_msg = ChatMessage(role=MessageRole.USER, content=user_msg)
messages = chat_history or []
if user_msg:
messages.append(user_msg)
return {
"messages": messages,
"tools": tool_specs or None,
"tool_choice": resolve_tool_choice(tool_choice) if tool_specs else None,
**kwargs,
}
def _validate_chat_with_tools_response(
self,
response: ChatResponse,
tools: List["BaseTool"],
allow_parallel_tool_calls: bool = False,
**kwargs: Any,
) -> ChatResponse:
"""Validate the response from chat_with_tools."""
if not allow_parallel_tool_calls:
force_single_tool_call(response)
return response
def get_tool_calls_from_response(
self,
response: "ChatResponse",
error_on_no_tool_call: bool = True,
**kwargs: Any,
) -> List[ToolSelection]:
"""Predict and call the tool."""
tool_calls = response.message.additional_kwargs.get("tool_calls", [])
if len(tool_calls) < 1:
if error_on_no_tool_call:
raise ValueError(
f"Expected at least one tool call, but got {len(tool_calls)} tool calls."
)
else:
return []
tool_selections = []
for tool_call in tool_calls:
if not isinstance(tool_call, get_args(OpenAIToolCall)):
raise ValueError("Invalid tool_call object")
if tool_call.type != "function":
raise ValueError("Invalid tool type. Unsupported by OpenAI")
# this should handle both complete and partial jsons
try:
argument_dict = parse_partial_json(tool_call.function.arguments)
except ValueError:
argument_dict = {}
tool_selections.append(
ToolSelection(
tool_id=tool_call.id,
tool_name=tool_call.function.name,
tool_kwargs=argument_dict,
)
)
return tool_selections
|