$npx -y skills add team-telnyx/ai --skill telnyx-ai-inference-pythonAccess Telnyx LLM inference APIs, embeddings, and AI analytics for call insights and summaries. This skill provides Python SDK examples.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx Ai Inference - Python |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | pip install telnyx |
| 9 | ``` |
| 10 | |
| 11 | ## Setup |
| 12 | |
| 13 | ```python |
| 14 | import os |
| 15 | from telnyx import Telnyx |
| 16 | |
| 17 | client = Telnyx( |
| 18 | api_key=os.environ.get("TELNYX_API_KEY"), # This is the default and can be omitted |
| 19 | ) |
| 20 | ``` |
| 21 | |
| 22 | All examples below assume `client` is already initialized as shown above. |
| 23 | |
| 24 | ## Error Handling |
| 25 | |
| 26 | All API calls can fail with network errors, rate limits (429), validation errors (422), |
| 27 | or authentication errors (401). Always handle errors in production code: |
| 28 | |
| 29 | ```python |
| 30 | import telnyx |
| 31 | |
| 32 | try: |
| 33 | result = client.messages.send(to="+13125550001", from_="+13125550002", text="Hello") |
| 34 | except telnyx.APIConnectionError: |
| 35 | print("Network error — check connectivity and retry") |
| 36 | except telnyx.RateLimitError: |
| 37 | # 429: rate limited — wait and retry with exponential backoff |
| 38 | import time |
| 39 | time.sleep(1) # Check Retry-After header for actual delay |
| 40 | except telnyx.APIStatusError as e: |
| 41 | print(f"API error {e.status_code}: {e.message}") |
| 42 | if e.status_code == 422: |
| 43 | print("Validation error — check required fields and formats") |
| 44 | ``` |
| 45 | |
| 46 | Common error codes: `401` invalid API key, `403` insufficient permissions, |
| 47 | `404` resource not found, `422` validation error (check field formats), |
| 48 | `429` rate limited (retry with exponential backoff). |
| 49 | |
| 50 | ## Important Notes |
| 51 | |
| 52 | - **Pagination:** List methods return an auto-paginating iterator. Use `for item in page_result:` to iterate through all pages automatically. |
| 53 | |
| 54 | ## Transcribe speech to text |
| 55 | |
| 56 | Transcribe speech to text. This endpoint is consistent with the [OpenAI Transcription API](https://platform.openai.com/docs/api-reference/audio/createTranscription) and may be used with the OpenAI JS or Python SDK. |
| 57 | |
| 58 | `POST /ai/audio/transcriptions` |
| 59 | |
| 60 | ```python |
| 61 | response = client.ai.audio.transcribe( |
| 62 | model="distil-whisper/distil-large-v2", |
| 63 | ) |
| 64 | print(response.text) |
| 65 | ``` |
| 66 | |
| 67 | Returns: `duration` (number), `segments` (array[object]), `text` (string) |
| 68 | |
| 69 | ## Create a chat completion |
| 70 | |
| 71 | Chat with a language model. This endpoint is consistent with the [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat) and may be used with the OpenAI JS or Python SDK. |
| 72 | |
| 73 | `POST /ai/chat/completions` — Required: `messages` |
| 74 | |
| 75 | Optional: `api_key_ref` (string), `best_of` (integer), `early_stopping` (boolean), `enable_thinking` (boolean), `frequency_penalty` (number), `guided_choice` (array[string]), `guided_json` (object), `guided_regex` (string), `length_penalty` (number), `logprobs` (boolean), `max_tokens` (integer), `min_p` (number), `model` (string), `n` (number), `presence_penalty` (number), `response_format` (object), `stream` (boolean), `temperature` (number), `tool_choice` (enum: none, auto, required), `tools` (array[object]), `top_logprobs` (integer), `top_p` (number), `use_beam_search` (boolean) |
| 76 | |
| 77 | ```python |
| 78 | response = client.ai.chat.create_completion( |
| 79 | messages=[{ |
| 80 | "role": "system", |
| 81 | "content": "You are a friendly chatbot.", |
| 82 | }, { |
| 83 | "role": "user", |
| 84 | "content": "Hello, world!", |
| 85 | }], |
| 86 | ) |
| 87 | print(response) |
| 88 | ``` |
| 89 | |
| 90 | ## List conversations |
| 91 | |
| 92 | Retrieve a list of all AI conversations configured by the user. Supports [PostgREST-style query parameters](https://postgrest.org/en/stable/api.html#horizontal-filtering-rows) for filtering. Examples are included for the standard metadata fields, but you can filter on any field in the metadata JSON object. |
| 93 | |
| 94 | `GET /ai/conversations` |
| 95 | |
| 96 | ```python |
| 97 | conversations = client.ai.conversations.list() |
| 98 | print(conversations.data) |
| 99 | ``` |
| 100 | |
| 101 | Returns: `created_at` (date-time), `id` (uuid), `last_message_at` (date-time), `metadata` (object), `name` (string) |
| 102 | |
| 103 | ## Create a conversation |
| 104 | |
| 105 | Create a new AI Conversation. |
| 106 | |
| 107 | `POST /ai/conversations` |
| 108 | |
| 109 | Optional: `metadata` (object), `name` (string) |
| 110 | |
| 111 | ```python |
| 112 | conversation = client.ai.conversations.create() |
| 113 | print(conversation.id) |
| 114 | ``` |
| 115 | |
| 116 | Returns: `created_at` (date-time), `id` (uuid), `last_message_at` (date-time), `metadata` (object), `name` (string) |
| 117 | |
| 118 | ## Get Insight Template Groups |
| 119 | |
| 120 | Get all insight groups |
| 121 | |
| 122 | `GET /ai/conversations/insight-groups` |
| 123 | |
| 124 | ```python |
| 125 | page = client.ai.conversations.insight_groups.retrieve_insight_groups() |
| 126 | page = page.data[0] |
| 127 | print(page.id) |
| 128 | ``` |
| 129 | |
| 130 | Returns: `created_at` (date-time), `description` (string), `id` (uuid), `insights` (array[object]), `name` (string), `webhook` (string) |
| 131 | |
| 132 | ## Create Insight Template Group |
| 133 | |
| 134 | Create a new insight group |
| 135 | |
| 136 | `POST /ai/conversations/insight-groups` — Required: `name` |
| 137 | |
| 138 | Optional: `description` (string), `webhook` (string) |
| 139 | |
| 140 | ```python |
| 141 | insight_template_group_detail = client.ai.conversations.insight_groups.insight_groups( |
| 142 | name="my-resource", |
| 143 | ) |
| 144 | print(insight_template_group_det |