mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-07 02:59:05 +00:00
347 lines
7.8 KiB
Text
Vendored
347 lines
7.8 KiB
Text
Vendored
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# LiteLLM CometAPI Cookbook"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install litellm"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Setup"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"\n",
|
|
"# CometAPI keys can be provided as COMETAPI_KEY or COMETAPI_API_KEY.\n",
|
|
"api_key = os.getenv(\"COMETAPI_KEY\") or os.getenv(\"COMETAPI_API_KEY\")\n",
|
|
"if api_key is None:\n",
|
|
" raise RuntimeError(\"Set COMETAPI_KEY or COMETAPI_API_KEY before running this notebook.\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Chat Completion"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from litellm import completion\n",
|
|
"\n",
|
|
"response = completion(\n",
|
|
" model=\"cometapi/gpt-5.5\",\n",
|
|
" messages=[{\"role\": \"user\", \"content\": \"Write Python code that prints hi.\"}],\n",
|
|
" max_tokens=128,\n",
|
|
" api_key=api_key,\n",
|
|
")\n",
|
|
"\n",
|
|
"print(response.choices[0].message.content)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Streaming Chat Completion"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"messages = [{\"role\": \"user\", \"content\": \"Count to three.\"}]\n",
|
|
"response = completion(\n",
|
|
" model=\"cometapi/gpt-5.5\",\n",
|
|
" messages=messages,\n",
|
|
" max_tokens=64,\n",
|
|
" stream=True,\n",
|
|
" api_key=api_key,\n",
|
|
")\n",
|
|
"\n",
|
|
"for part in response:\n",
|
|
" print(part.choices[0].delta.content or \"\", end=\"\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Async Chat Completion"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from litellm import acompletion\n",
|
|
"\n",
|
|
"async def run_async_chat():\n",
|
|
" response = await acompletion(\n",
|
|
" model=\"cometapi/gpt-5.5\",\n",
|
|
" messages=[{\"content\": \"Hello, how are you?\", \"role\": \"user\"}],\n",
|
|
" max_tokens=64,\n",
|
|
" api_key=api_key,\n",
|
|
" )\n",
|
|
" return response\n",
|
|
"\n",
|
|
"response = await run_async_chat()\n",
|
|
"print(response.choices[0].message.content)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Async Streaming Chat Completion"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"async def run_async_streaming_chat():\n",
|
|
" response = await acompletion(\n",
|
|
" model=\"cometapi/gpt-5.5\",\n",
|
|
" messages=[{\"content\": \"Stream one short sentence.\", \"role\": \"user\"}],\n",
|
|
" max_tokens=64,\n",
|
|
" stream=True,\n",
|
|
" api_key=api_key,\n",
|
|
" )\n",
|
|
" async for chunk in response:\n",
|
|
" print(chunk.choices[0].delta.content or \"\", end=\"\")\n",
|
|
"\n",
|
|
"await run_async_streaming_chat()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Embeddings"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import litellm\n",
|
|
"\n",
|
|
"response = litellm.embedding(\n",
|
|
" model=\"cometapi/text-embedding-3-small\",\n",
|
|
" input=[\"LiteLLM routes this embedding request through CometAPI.\"],\n",
|
|
" api_key=api_key,\n",
|
|
")\n",
|
|
"\n",
|
|
"print(len(response.data[0][\"embedding\"]))\n",
|
|
"print(response.usage)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Async Embeddings"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"async def run_async_embedding():\n",
|
|
" response = await litellm.aembedding(\n",
|
|
" model=\"cometapi/text-embedding-3-small\",\n",
|
|
" input=\"Your text string\",\n",
|
|
" api_key=api_key,\n",
|
|
" )\n",
|
|
" return response\n",
|
|
"\n",
|
|
"response = await run_async_embedding()\n",
|
|
"print(len(response.data[0][\"embedding\"]))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Image Generation"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"response = litellm.image_generation(\n",
|
|
" model=\"cometapi/gpt-image-2\",\n",
|
|
" prompt=\"A small comet over a clean API diagram\",\n",
|
|
" size=\"1024x1024\",\n",
|
|
" api_key=api_key,\n",
|
|
")\n",
|
|
"\n",
|
|
"image = response.data[0]\n",
|
|
"print(image.url or image.b64_json[:64])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Async Image Generation"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"async def run_async_image_generation():\n",
|
|
" response = await litellm.aimage_generation(\n",
|
|
" model=\"cometapi/gpt-image-2\",\n",
|
|
" prompt=\"A cute baby sea otter\",\n",
|
|
" size=\"1024x1024\",\n",
|
|
" api_key=api_key,\n",
|
|
" )\n",
|
|
" return response\n",
|
|
"\n",
|
|
"response = await run_async_image_generation()\n",
|
|
"image = response.data[0]\n",
|
|
"print(image.url or image.b64_json[:64])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Audio Speech"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import io\n",
|
|
"\n",
|
|
"speech_response = litellm.speech(\n",
|
|
" model=\"cometapi/tts-1\",\n",
|
|
" input=\"LiteLLM can route speech generation through CometAPI.\",\n",
|
|
" voice=\"alloy\",\n",
|
|
" api_key=api_key,\n",
|
|
")\n",
|
|
"\n",
|
|
"speech_audio = io.BytesIO(speech_response.content)\n",
|
|
"speech_audio.name = \"speech.mp3\"\n",
|
|
"\n",
|
|
"print(len(speech_response.content))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Audio Transcription"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"speech_audio.seek(0)\n",
|
|
"transcription_response = litellm.transcription(\n",
|
|
" model=\"cometapi/whisper-1\",\n",
|
|
" file=speech_audio,\n",
|
|
" api_key=api_key,\n",
|
|
")\n",
|
|
"\n",
|
|
"print(transcription_response.text)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Moderations"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"moderation_response = litellm.moderation(\n",
|
|
" model=\"omni-moderation-latest\",\n",
|
|
" custom_llm_provider=\"cometapi\",\n",
|
|
" input=\"I want to build a safe application.\",\n",
|
|
" api_key=api_key,\n",
|
|
")\n",
|
|
"\n",
|
|
"print(moderation_response.results[0].categories)\n",
|
|
"print(moderation_response.results[0].category_scores)\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"provenance": []
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.12.8"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|