0
# Embeddings & Other APIs
1
2
Specialized API endpoints for embedding generation, image creation, audio processing, moderation, and other non-completion services. These functions provide unified interfaces for diverse AI capabilities across multiple providers.
3
4
## Capabilities
5
6
### Embedding Generation
7
8
Generate vector embeddings from text inputs using various embedding models across different providers.
9
10
```python { .api }
11
def embedding(
12
model: str,
13
input: Union[str, List[str], List[int], List[List[int]]],
14
# Optional parameters
15
encoding_format: Optional[Literal["float", "base64"]] = None,
16
dimensions: Optional[int] = None,
17
user: Optional[str] = None,
18
# LiteLLM specific
19
timeout: Optional[float] = None,
20
api_key: Optional[str] = None,
21
api_base: Optional[str] = None,
22
api_version: Optional[str] = None,
23
custom_llm_provider: Optional[str] = None,
24
**kwargs
25
) -> EmbeddingResponse:
26
"""
27
Generate embeddings for input text using specified embedding model.
28
29
Args:
30
model (str): Embedding model identifier (e.g., "text-embedding-ada-002", "embed-english-v3.0")
31
input (Union[str, List[str], List[int], List[List[int]]]): Text or tokens to embed
32
encoding_format (Optional[str]): Format for embedding values ("float" or "base64")
33
dimensions (Optional[int]): Number of dimensions for embedding (if supported)
34
user (Optional[str]): User identifier for tracking
35
timeout (Optional[float]): Request timeout in seconds
36
api_key (Optional[str]): Provider API key override
37
38
Returns:
39
EmbeddingResponse: Embedding vectors with usage information
40
41
Raises:
42
AuthenticationError: Invalid API key
43
InvalidRequestError: Invalid model or parameters
44
RateLimitError: Rate limit exceeded
45
"""
46
47
async def aembedding(
48
model: str,
49
input: Union[str, List[str], List[int], List[List[int]]],
50
**kwargs
51
) -> EmbeddingResponse:
52
"""
53
Async version of embedding generation.
54
55
Args:
56
Same as embedding() function
57
58
Returns:
59
EmbeddingResponse: Async embedding response
60
"""
61
```
62
63
### Image Generation
64
65
Create images from text prompts using various image generation models.
66
67
```python { .api }
68
def image_generation(
69
prompt: str,
70
model: Optional[str] = None,
71
# Generation parameters
72
n: Optional[int] = None,
73
quality: Optional[Literal["standard", "hd"]] = None,
74
response_format: Optional[Literal["url", "b64_json"]] = None,
75
size: Optional[Literal["256x256", "512x512", "1024x1024", "1792x1024", "1024x1792"]] = None,
76
style: Optional[Literal["vivid", "natural"]] = None,
77
user: Optional[str] = None,
78
# LiteLLM specific
79
timeout: Optional[float] = None,
80
api_key: Optional[str] = None,
81
custom_llm_provider: Optional[str] = None,
82
**kwargs
83
) -> ImageResponse:
84
"""
85
Generate images from text prompts.
86
87
Args:
88
prompt (str): Text description of desired image
89
model (Optional[str]): Image generation model (e.g., "dall-e-3", "dall-e-2")
90
n (Optional[int]): Number of images to generate
91
quality (Optional[str]): Image quality level ("standard" or "hd")
92
size (Optional[str]): Image dimensions
93
style (Optional[str]): Image style ("vivid" or "natural")
94
response_format (Optional[str]): Return format ("url" or "b64_json")
95
96
Returns:
97
ImageResponse: Generated image URLs or base64 data
98
99
Raises:
100
ContentPolicyViolationError: Prompt violates content policy
101
InvalidRequestError: Invalid parameters or model
102
"""
103
104
async def aimage_generation(
105
prompt: str,
106
**kwargs
107
) -> ImageResponse:
108
"""
109
Async image generation.
110
111
Args:
112
Same as image_generation() function
113
114
Returns:
115
ImageResponse: Async image generation response
116
"""
117
```
118
119
### Audio Transcription
120
121
Convert audio files to text using speech-to-text models.
122
123
```python { .api }
124
def transcription(
125
model: str,
126
file: Union[str, bytes, IO],
127
# Transcription parameters
128
language: Optional[str] = None,
129
prompt: Optional[str] = None,
130
response_format: Optional[Literal["json", "text", "srt", "verbose_json", "vtt"]] = None,
131
temperature: Optional[float] = None,
132
timestamp_granularities: Optional[List[Literal["word", "segment"]]] = None,
133
# LiteLLM specific
134
timeout: Optional[float] = None,
135
api_key: Optional[str] = None,
136
custom_llm_provider: Optional[str] = None,
137
**kwargs
138
) -> TranscriptionResponse:
139
"""
140
Transcribe audio to text.
141
142
Args:
143
model (str): Transcription model (e.g., "whisper-1")
144
file (Union[str, bytes, IO]): Audio file path, bytes, or file object
145
language (Optional[str]): Audio language code (ISO-639-1)
146
prompt (Optional[str]): Optional text to guide transcription
147
response_format (Optional[str]): Output format ("json", "text", "srt", etc.)
148
temperature (Optional[float]): Sampling temperature (0.0 to 1.0)
149
timestamp_granularities (Optional[List[str]]): Timestamp detail level
150
151
Returns:
152
TranscriptionResponse: Transcribed text with metadata
153
154
Raises:
155
InvalidRequestError: Unsupported file format or invalid parameters
156
APIError: Transcription service error
157
"""
158
159
async def atranscription(
160
model: str,
161
file: Union[str, bytes, IO],
162
**kwargs
163
) -> TranscriptionResponse:
164
"""
165
Async audio transcription.
166
167
Args:
168
Same as transcription() function
169
170
Returns:
171
TranscriptionResponse: Async transcription response
172
"""
173
```
174
175
### Audio Speech Synthesis
176
177
Generate speech audio from text using text-to-speech models.
178
179
```python { .api }
180
def speech(
181
model: str,
182
input: str,
183
voice: str,
184
# Speech parameters
185
response_format: Optional[Literal["mp3", "opus", "aac", "flac", "wav", "pcm"]] = None,
186
speed: Optional[float] = None,
187
# LiteLLM specific
188
timeout: Optional[float] = None,
189
api_key: Optional[str] = None,
190
custom_llm_provider: Optional[str] = None,
191
**kwargs
192
) -> bytes:
193
"""
194
Generate speech audio from text.
195
196
Args:
197
model (str): Speech synthesis model (e.g., "tts-1", "tts-1-hd")
198
input (str): Text to convert to speech
199
voice (str): Voice identifier ("alloy", "echo", "fable", "onyx", "nova", "shimmer")
200
response_format (Optional[str]): Audio format ("mp3", "opus", "aac", etc.)
201
speed (Optional[float]): Playback speed (0.25 to 4.0)
202
203
Returns:
204
bytes: Generated audio data
205
206
Raises:
207
InvalidRequestError: Invalid voice or parameters
208
APIError: Speech synthesis error
209
"""
210
211
async def aspeech(
212
model: str,
213
input: str,
214
voice: str,
215
**kwargs
216
) -> bytes:
217
"""
218
Async speech synthesis.
219
220
Args:
221
Same as speech() function
222
223
Returns:
224
bytes: Async speech audio data
225
"""
226
```
227
228
### Content Moderation
229
230
Detect potentially harmful or inappropriate content in text.
231
232
```python { .api }
233
def moderation(
234
input: Union[str, List[str]],
235
model: Optional[str] = None,
236
# LiteLLM specific
237
timeout: Optional[float] = None,
238
api_key: Optional[str] = None,
239
custom_llm_provider: Optional[str] = None,
240
**kwargs
241
) -> ModerationCreateResponse:
242
"""
243
Check content for policy violations.
244
245
Args:
246
input (Union[str, List[str]]): Text or list of texts to moderate
247
model (Optional[str]): Moderation model (e.g., "text-moderation-latest")
248
timeout (Optional[float]): Request timeout in seconds
249
250
Returns:
251
ModerationCreateResponse: Moderation results with category flags
252
253
Raises:
254
InvalidRequestError: Invalid input format
255
APIError: Moderation service error
256
"""
257
258
async def amoderation(
259
input: Union[str, List[str]],
260
**kwargs
261
) -> ModerationCreateResponse:
262
"""
263
Async content moderation.
264
265
Args:
266
Same as moderation() function
267
268
Returns:
269
ModerationCreateResponse: Async moderation response
270
"""
271
```
272
273
### Reranking
274
275
Reorder documents by relevance to a query using reranking models.
276
277
```python { .api }
278
def rerank(
279
model: str,
280
query: str,
281
documents: List[Union[str, Dict[str, Any]]],
282
top_n: Optional[int] = None,
283
return_documents: Optional[bool] = None,
284
# LiteLLM specific
285
timeout: Optional[float] = None,
286
api_key: Optional[str] = None,
287
custom_llm_provider: Optional[str] = None,
288
**kwargs
289
) -> RerankResponse:
290
"""
291
Rerank documents by relevance to query.
292
293
Args:
294
model (str): Reranking model (e.g., "rerank-english-v3.0")
295
query (str): Search query
296
documents (List[Union[str, Dict]]): Documents to rank
297
top_n (Optional[int]): Number of top results to return
298
return_documents (Optional[bool]): Include document content in response
299
300
Returns:
301
RerankResponse: Ranked documents with relevance scores
302
303
Raises:
304
InvalidRequestError: Invalid documents or parameters
305
"""
306
307
async def arerank(
308
model: str,
309
query: str,
310
documents: List[Union[str, Dict[str, Any]]],
311
**kwargs
312
) -> RerankResponse:
313
"""
314
Async document reranking.
315
316
Args:
317
Same as rerank() function
318
319
Returns:
320
RerankResponse: Async reranking response
321
"""
322
```
323
324
## Response Objects
325
326
```python { .api }
327
class EmbeddingResponse(OpenAIObject):
328
"""Embedding generation response"""
329
object: str = "list"
330
data: List[EmbeddingData]
331
model: Optional[str]
332
usage: Optional[Usage]
333
_hidden_params: HiddenParams = {}
334
335
class EmbeddingData:
336
"""Individual embedding vector"""
337
object: str = "embedding"
338
index: int
339
embedding: List[float]
340
341
class ImageResponse(OpenAIObject):
342
"""Image generation response"""
343
created: int
344
data: List[ImageObject]
345
usage: Optional[Usage] = None
346
_hidden_params: HiddenParams = {}
347
348
class ImageObject:
349
"""Individual generated image"""
350
b64_json: Optional[str] = None
351
url: Optional[str] = None
352
revised_prompt: Optional[str] = None
353
354
class TranscriptionResponse(OpenAIObject):
355
"""Audio transcription response"""
356
text: str
357
task: Optional[str] = None
358
language: Optional[str] = None
359
duration: Optional[float] = None
360
segments: Optional[List[TranscriptionSegment]] = None
361
words: Optional[List[TranscriptionWord]] = None
362
_hidden_params: HiddenParams = {}
363
364
class TranscriptionSegment:
365
"""Transcription segment with timestamp"""
366
id: int
367
seek: int
368
start: float
369
end: float
370
text: str
371
tokens: List[int]
372
temperature: float
373
avg_logprob: float
374
compression_ratio: float
375
no_speech_prob: float
376
377
class TranscriptionWord:
378
"""Individual word with timestamp"""
379
word: str
380
start: float
381
end: float
382
383
class ModerationCreateResponse(OpenAIObject):
384
"""Content moderation response"""
385
id: str
386
model: str
387
results: List[ModerationObject]
388
389
class ModerationObject:
390
"""Individual moderation result"""
391
flagged: bool
392
categories: ModerationCategories
393
category_scores: ModerationCategoryScores
394
395
class ModerationCategories:
396
"""Moderation category flags"""
397
hate: bool
398
hate_threatening: bool
399
harassment: bool
400
harassment_threatening: bool
401
self_harm: bool
402
self_harm_intent: bool
403
self_harm_instructions: bool
404
sexual: bool
405
sexual_minors: bool
406
violence: bool
407
violence_graphic: bool
408
409
class ModerationCategoryScores:
410
"""Moderation confidence scores"""
411
hate: float
412
hate_threatening: float
413
harassment: float
414
harassment_threatening: float
415
self_harm: float
416
self_harm_intent: float
417
self_harm_instructions: float
418
sexual: float
419
sexual_minors: float
420
violence: float
421
violence_graphic: float
422
423
class RerankResponse(OpenAIObject):
424
"""Document reranking response"""
425
id: Optional[str] = None
426
results: List[RerankResult]
427
meta: Optional[Dict[str, Any]] = None
428
usage: Optional[Usage] = None
429
430
class RerankResult:
431
"""Individual reranked document"""
432
index: int
433
relevance_score: float
434
document: Optional[Dict[str, Any]] = None
435
```
436
437
## Usage Examples
438
439
### Embedding Generation
440
441
```python
442
import litellm
443
444
# Single text embedding
445
response = litellm.embedding(
446
model="text-embedding-ada-002",
447
input="Hello, world!"
448
)
449
450
embedding_vector = response.data[0].embedding
451
print(f"Embedding dimensions: {len(embedding_vector)}")
452
453
# Batch embedding generation
454
texts = [
455
"First document text",
456
"Second document text",
457
"Third document text"
458
]
459
460
response = litellm.embedding(
461
model="text-embedding-ada-002",
462
input=texts
463
)
464
465
for i, data in enumerate(response.data):
466
print(f"Document {i}: {len(data.embedding)} dimensions")
467
468
# Different embedding providers
469
cohere_response = litellm.embedding(
470
model="embed-english-v3.0",
471
input="Text for Cohere embedding"
472
)
473
474
anthropic_response = litellm.embedding(
475
model="voyage-large-2",
476
input="Text for Voyage embedding"
477
)
478
```
479
480
### Image Generation
481
482
```python
483
# Basic image generation
484
response = litellm.image_generation(
485
prompt="A futuristic cityscape at sunset",
486
model="dall-e-3",
487
size="1024x1024",
488
quality="hd"
489
)
490
491
image_url = response.data[0].url
492
print(f"Generated image: {image_url}")
493
494
# Multiple images
495
response = litellm.image_generation(
496
prompt="A cute robot assistant",
497
model="dall-e-2",
498
n=4,
499
size="512x512"
500
)
501
502
for i, image in enumerate(response.data):
503
print(f"Image {i}: {image.url}")
504
505
# Base64 format
506
response = litellm.image_generation(
507
prompt="Abstract art with vibrant colors",
508
model="dall-e-3",
509
response_format="b64_json"
510
)
511
512
image_data = response.data[0].b64_json
513
# Save base64 image data to file
514
```
515
516
### Audio Transcription
517
518
```python
519
# Transcribe audio file
520
with open("audio.mp3", "rb") as audio_file:
521
response = litellm.transcription(
522
model="whisper-1",
523
file=audio_file,
524
language="en",
525
response_format="verbose_json",
526
timestamp_granularities=["word", "segment"]
527
)
528
529
print("Transcription:", response.text)
530
print("Language:", response.language)
531
print("Duration:", response.duration)
532
533
# Process segments with timestamps
534
for segment in response.segments:
535
print(f"[{segment.start:.2f}-{segment.end:.2f}]: {segment.text}")
536
537
# Process individual words
538
for word in response.words:
539
print(f"{word.word} ({word.start:.2f}-{word.end:.2f})")
540
541
# Different response formats
542
srt_response = litellm.transcription(
543
model="whisper-1",
544
file="audio.mp3",
545
response_format="srt"
546
)
547
print("SRT format:", srt_response.text)
548
```
549
550
### Speech Synthesis
551
552
```python
553
# Generate speech from text
554
audio_data = litellm.speech(
555
model="tts-1",
556
input="Hello, this is a test of text-to-speech synthesis.",
557
voice="alloy",
558
response_format="mp3"
559
)
560
561
# Save audio to file
562
with open("output.mp3", "wb") as f:
563
f.write(audio_data)
564
565
# Different voices and formats
566
voices = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
567
568
for voice in voices:
569
audio = litellm.speech(
570
model="tts-1-hd",
571
input=f"This is the {voice} voice.",
572
voice=voice,
573
response_format="wav",
574
speed=1.2
575
)
576
577
with open(f"voice_{voice}.wav", "wb") as f:
578
f.write(audio)
579
```
580
581
### Content Moderation
582
583
```python
584
# Single text moderation
585
response = litellm.moderation(
586
input="This is a sample text to check for policy violations."
587
)
588
589
result = response.results[0]
590
if result.flagged:
591
print("Content flagged for policy violation")
592
for category, flagged in result.categories.__dict__.items():
593
if flagged:
594
score = getattr(result.category_scores, category)
595
print(f" {category}: {score:.3f}")
596
597
# Batch moderation
598
texts = [
599
"First text to moderate",
600
"Second text to moderate",
601
"Third text to moderate"
602
]
603
604
response = litellm.moderation(input=texts)
605
606
for i, result in enumerate(response.results):
607
print(f"Text {i}: {'FLAGGED' if result.flagged else 'OK'}")
608
```
609
610
## OpenAI API Compatibility
611
612
LiteLLM provides complete compatibility with OpenAI's additional APIs including Assistants, Batch processing, Files, and Fine-tuning. These functions maintain identical signatures and behavior to OpenAI's API.
613
614
### Assistants API
615
616
Create and manage AI assistants with custom instructions, knowledge bases, and tool capabilities.
617
618
```python { .api }
619
def get_assistants(
620
limit: Optional[int] = None,
621
order: Optional[str] = None,
622
after: Optional[str] = None,
623
before: Optional[str] = None,
624
**kwargs
625
) -> AssistantList:
626
"""List assistants in the organization."""
627
628
async def aget_assistants(**kwargs) -> AssistantList:
629
"""Async version of get_assistants."""
630
631
def create_assistants(
632
model: str,
633
name: Optional[str] = None,
634
description: Optional[str] = None,
635
instructions: Optional[str] = None,
636
tools: Optional[List[Dict[str, Any]]] = None,
637
tool_resources: Optional[Dict[str, Any]] = None,
638
metadata: Optional[Dict[str, str]] = None,
639
temperature: Optional[float] = None,
640
top_p: Optional[float] = None,
641
response_format: Optional[Dict[str, Any]] = None,
642
**kwargs
643
) -> Assistant:
644
"""Create a new assistant."""
645
646
async def acreate_assistants(**kwargs) -> Assistant:
647
"""Async version of create_assistants."""
648
649
def delete_assistant(assistant_id: str, **kwargs) -> DeletionStatus:
650
"""Delete an assistant."""
651
652
async def adelete_assistant(assistant_id: str, **kwargs) -> DeletionStatus:
653
"""Async version of delete_assistant."""
654
655
def create_thread(
656
messages: Optional[List[Dict[str, Any]]] = None,
657
tool_resources: Optional[Dict[str, Any]] = None,
658
metadata: Optional[Dict[str, str]] = None,
659
**kwargs
660
) -> Thread:
661
"""Create a conversation thread."""
662
663
async def acreate_thread(**kwargs) -> Thread:
664
"""Async version of create_thread."""
665
666
def get_thread(thread_id: str, **kwargs) -> Thread:
667
"""Retrieve a thread by ID."""
668
669
async def aget_thread(thread_id: str, **kwargs) -> Thread:
670
"""Async version of get_thread."""
671
672
def add_message(
673
thread_id: str,
674
role: str,
675
content: str,
676
attachments: Optional[List[Dict[str, Any]]] = None,
677
metadata: Optional[Dict[str, str]] = None,
678
**kwargs
679
) -> ThreadMessage:
680
"""Add a message to a thread."""
681
682
async def a_add_message(**kwargs) -> ThreadMessage:
683
"""Async version of add_message."""
684
685
def get_messages(
686
thread_id: str,
687
limit: Optional[int] = None,
688
order: Optional[str] = None,
689
after: Optional[str] = None,
690
before: Optional[str] = None,
691
run_id: Optional[str] = None,
692
**kwargs
693
) -> ThreadMessageList:
694
"""Get messages from a thread."""
695
696
async def aget_messages(**kwargs) -> ThreadMessageList:
697
"""Async version of get_messages."""
698
699
def run_thread(
700
thread_id: str,
701
assistant_id: str,
702
model: Optional[str] = None,
703
instructions: Optional[str] = None,
704
additional_instructions: Optional[str] = None,
705
additional_messages: Optional[List[Dict[str, Any]]] = None,
706
tools: Optional[List[Dict[str, Any]]] = None,
707
metadata: Optional[Dict[str, str]] = None,
708
temperature: Optional[float] = None,
709
top_p: Optional[float] = None,
710
stream: Optional[bool] = None,
711
max_prompt_tokens: Optional[int] = None,
712
max_completion_tokens: Optional[int] = None,
713
truncation_strategy: Optional[Dict[str, Any]] = None,
714
tool_choice: Optional[Union[str, Dict[str, Any]]] = None,
715
parallel_tool_calls: Optional[bool] = None,
716
response_format: Optional[Dict[str, Any]] = None,
717
**kwargs
718
) -> Run:
719
"""Run an assistant on a thread."""
720
721
async def arun_thread(**kwargs) -> Run:
722
"""Async version of run_thread."""
723
724
def run_thread_stream(
725
thread_id: str,
726
assistant_id: str,
727
**kwargs
728
) -> Iterator[AssistantEventHandler]:
729
"""Stream assistant run events."""
730
731
async def arun_thread_stream(**kwargs) -> AsyncIterator[AssistantEventHandler]:
732
"""Async version of run_thread_stream."""
733
```
734
735
### Batch API
736
737
Process multiple requests in batches for cost-effective bulk operations.
738
739
```python { .api }
740
def create_batch(
741
input_file_id: str,
742
endpoint: str,
743
completion_window: str = "24h",
744
metadata: Optional[Dict[str, str]] = None,
745
**kwargs
746
) -> Batch:
747
"""Create a batch processing job."""
748
749
def retrieve_batch(batch_id: str, **kwargs) -> Batch:
750
"""Retrieve batch job status and results."""
751
752
def list_batches(
753
after: Optional[str] = None,
754
limit: Optional[int] = None,
755
**kwargs
756
) -> BatchList:
757
"""List batch processing jobs."""
758
759
def cancel_batch(batch_id: str, **kwargs) -> Batch:
760
"""Cancel a batch processing job."""
761
```
762
763
### Files API
764
765
Upload, manage, and process files for use with other OpenAI services.
766
767
```python { .api }
768
def create_file(
769
file: Union[str, bytes, IO],
770
purpose: str,
771
**kwargs
772
) -> FileObject:
773
"""Upload a file for use with other services."""
774
775
async def acreate_file(**kwargs) -> FileObject:
776
"""Async version of create_file."""
777
778
def file_retrieve(file_id: str, **kwargs) -> FileObject:
779
"""Retrieve file information."""
780
781
async def afile_retrieve(file_id: str, **kwargs) -> FileObject:
782
"""Async version of file_retrieve."""
783
784
def file_delete(file_id: str, **kwargs) -> DeletionStatus:
785
"""Delete a file."""
786
787
async def afile_delete(file_id: str, **kwargs) -> DeletionStatus:
788
"""Async version of file_delete."""
789
790
def file_list(
791
purpose: Optional[str] = None,
792
after: Optional[str] = None,
793
limit: Optional[int] = None,
794
order: Optional[str] = None,
795
**kwargs
796
) -> FileList:
797
"""List uploaded files."""
798
799
async def afile_list(**kwargs) -> FileList:
800
"""Async version of file_list."""
801
802
def file_content(file_id: str, **kwargs) -> bytes:
803
"""Retrieve file content."""
804
805
async def afile_content(file_id: str, **kwargs) -> bytes:
806
"""Async version of file_content."""
807
```
808
809
### Fine-tuning API
810
811
Create and manage fine-tuning jobs for custom model training.
812
813
```python { .api }
814
def create_fine_tuning_job(
815
training_file: str,
816
model: str,
817
hyperparameters: Optional[Dict[str, Any]] = None,
818
suffix: Optional[str] = None,
819
validation_file: Optional[str] = None,
820
integrations: Optional[List[Dict[str, Any]]] = None,
821
seed: Optional[int] = None,
822
**kwargs
823
) -> FineTuningJob:
824
"""Create a fine-tuning job."""
825
826
async def acreate_fine_tuning_job(**kwargs) -> FineTuningJob:
827
"""Async version of create_fine_tuning_job."""
828
829
def cancel_fine_tuning_job(fine_tuning_job_id: str, **kwargs) -> FineTuningJob:
830
"""Cancel a fine-tuning job."""
831
832
async def acancel_fine_tuning_job(fine_tuning_job_id: str, **kwargs) -> FineTuningJob:
833
"""Async version of cancel_fine_tuning_job."""
834
835
def list_fine_tuning_jobs(
836
after: Optional[str] = None,
837
limit: Optional[int] = None,
838
**kwargs
839
) -> FineTuningJobList:
840
"""List fine-tuning jobs."""
841
842
async def alist_fine_tuning_jobs(**kwargs) -> FineTuningJobList:
843
"""Async version of list_fine_tuning_jobs."""
844
845
def retrieve_fine_tuning_job(fine_tuning_job_id: str, **kwargs) -> FineTuningJob:
846
"""Retrieve fine-tuning job details."""
847
848
async def aretrieve_fine_tuning_job(fine_tuning_job_id: str, **kwargs) -> FineTuningJob:
849
"""Async version of retrieve_fine_tuning_job."""
850
```
851
852
## OpenAI Compatibility Response Objects
853
854
```python { .api }
855
class Assistant(OpenAIObject):
856
"""AI Assistant configuration"""
857
id: str
858
object: str = "assistant"
859
created_at: int
860
name: Optional[str]
861
description: Optional[str]
862
model: str
863
instructions: Optional[str]
864
tools: List[Dict[str, Any]]
865
tool_resources: Optional[Dict[str, Any]]
866
metadata: Dict[str, str]
867
temperature: Optional[float]
868
top_p: Optional[float]
869
response_format: Optional[Dict[str, Any]]
870
871
class Thread(OpenAIObject):
872
"""Conversation thread"""
873
id: str
874
object: str = "thread"
875
created_at: int
876
tool_resources: Optional[Dict[str, Any]]
877
metadata: Dict[str, str]
878
879
class ThreadMessage(OpenAIObject):
880
"""Message in a conversation thread"""
881
id: str
882
object: str = "thread.message"
883
created_at: int
884
assistant_id: Optional[str]
885
thread_id: str
886
run_id: Optional[str]
887
role: str
888
content: List[Dict[str, Any]]
889
attachments: Optional[List[Dict[str, Any]]]
890
metadata: Dict[str, str]
891
892
class Run(OpenAIObject):
893
"""Assistant run on a thread"""
894
id: str
895
object: str = "thread.run"
896
created_at: int
897
assistant_id: str
898
thread_id: str
899
status: str
900
started_at: Optional[int]
901
expires_at: Optional[int]
902
cancelled_at: Optional[int]
903
failed_at: Optional[int]
904
completed_at: Optional[int]
905
required_action: Optional[Dict[str, Any]]
906
last_error: Optional[Dict[str, Any]]
907
model: str
908
instructions: str
909
tools: List[Dict[str, Any]]
910
metadata: Dict[str, str]
911
usage: Optional[Usage]
912
temperature: Optional[float]
913
top_p: Optional[float]
914
max_completion_tokens: Optional[int]
915
max_prompt_tokens: Optional[int]
916
truncation_strategy: Optional[Dict[str, Any]]
917
incomplete_details: Optional[Dict[str, Any]]
918
tool_choice: Optional[Union[str, Dict[str, Any]]]
919
parallel_tool_calls: Optional[bool]
920
response_format: Optional[Dict[str, Any]]
921
922
class Batch(OpenAIObject):
923
"""Batch processing job"""
924
id: str
925
object: str = "batch"
926
endpoint: str
927
errors: Optional[Dict[str, Any]]
928
input_file_id: str
929
completion_window: str
930
status: str
931
output_file_id: Optional[str]
932
error_file_id: Optional[str]
933
created_at: int
934
in_progress_at: Optional[int]
935
expires_at: Optional[int]
936
finalizing_at: Optional[int]
937
completed_at: Optional[int]
938
failed_at: Optional[int]
939
expired_at: Optional[int]
940
cancelling_at: Optional[int]
941
cancelled_at: Optional[int]
942
request_counts: Optional[Dict[str, int]]
943
metadata: Optional[Dict[str, str]]
944
945
class FileObject(OpenAIObject):
946
"""Uploaded file"""
947
id: str
948
object: str = "file"
949
bytes: int
950
created_at: int
951
filename: str
952
purpose: str
953
status: Optional[str]
954
status_details: Optional[str]
955
956
class FineTuningJob(OpenAIObject):
957
"""Fine-tuning job"""
958
id: str
959
object: str = "fine_tuning.job"
960
created_at: int
961
error: Optional[Dict[str, Any]]
962
fine_tuned_model: Optional[str]
963
finished_at: Optional[int]
964
hyperparameters: Dict[str, Any]
965
model: str
966
organization_id: str
967
result_files: List[str]
968
seed: Optional[int]
969
status: str
970
trained_tokens: Optional[int]
971
training_file: str
972
validation_file: Optional[str]
973
integrations: Optional[List[Dict[str, Any]]]
974
estimated_finish: Optional[int]
975
976
class DeletionStatus(OpenAIObject):
977
"""Deletion confirmation"""
978
id: str
979
object: str
980
deleted: bool
981
```
982
983
### Document Reranking
984
985
```python
986
# Rerank documents by relevance
987
query = "machine learning algorithms"
988
documents = [
989
"Introduction to neural networks and deep learning",
990
"Statistical methods in data analysis",
991
"Computer vision with convolutional networks",
992
"Natural language processing fundamentals",
993
"Supervised learning algorithms overview"
994
]
995
996
response = litellm.rerank(
997
model="rerank-english-v3.0",
998
query=query,
999
documents=documents,
1000
top_n=3,
1001
return_documents=True
1002
)
1003
1004
print("Reranked results:")
1005
for result in response.results:
1006
doc_text = documents[result.index]
1007
print(f"Score: {result.relevance_score:.3f} - {doc_text}")
1008
1009
# Rerank with structured documents
1010
documents = [
1011
{"id": "doc1", "title": "ML Overview", "content": "Machine learning fundamentals"},
1012
{"id": "doc2", "title": "Statistics", "content": "Statistical analysis methods"},
1013
{"id": "doc3", "title": "Deep Learning", "content": "Neural network architectures"}
1014
]
1015
1016
response = litellm.rerank(
1017
model="rerank-english-v3.0",
1018
query="neural networks",
1019
documents=documents,
1020
return_documents=True
1021
)
1022
```
1023
1024
### Async Operations
1025
1026
```python
1027
import asyncio
1028
1029
async def process_multiple_apis():
1030
# Concurrent API calls
1031
embedding_task = litellm.aembedding(
1032
model="text-embedding-ada-002",
1033
input="Text to embed"
1034
)
1035
1036
image_task = litellm.aimage_generation(
1037
prompt="A beautiful landscape",
1038
model="dall-e-3"
1039
)
1040
1041
transcription_task = litellm.atranscription(
1042
model="whisper-1",
1043
file="audio.mp3"
1044
)
1045
1046
# Wait for all tasks to complete
1047
embedding_resp, image_resp, transcription_resp = await asyncio.gather(
1048
embedding_task, image_task, transcription_task
1049
)
1050
1051
return {
1052
"embedding": embedding_resp.data[0].embedding,
1053
"image_url": image_resp.data[0].url,
1054
"transcription": transcription_resp.text
1055
}
1056
1057
results = asyncio.run(process_multiple_apis())
1058
```
1059
1060
### Provider-Specific Usage
1061
1062
```python
1063
# OpenAI embedding with specific dimensions
1064
openai_embedding = litellm.embedding(
1065
model="text-embedding-3-large",
1066
input="Sample text",
1067
dimensions=1024 # Reduced dimensions
1068
)
1069
1070
# Cohere embedding with specific input type
1071
cohere_embedding = litellm.embedding(
1072
model="embed-english-v3.0",
1073
input="Sample text",
1074
custom_llm_provider="cohere",
1075
input_type="search_document" # Cohere-specific parameter
1076
)
1077
1078
# Azure OpenAI image generation
1079
azure_image = litellm.image_generation(
1080
prompt="Digital art",
1081
model="azure/dall-e-3",
1082
api_base="https://my-resource.openai.azure.com/",
1083
api_version="2024-02-01",
1084
api_key="azure-api-key"
1085
)
1086
```