$npx -y skills add team-telnyx/ai --skill telnyx-ai-inference-javaAccess Telnyx LLM inference APIs, embeddings, and AI analytics for call insights and summaries. This skill provides Java SDK examples.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx Ai Inference - Java |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```text |
| 8 | <!-- Maven --> |
| 9 | <dependency> |
| 10 | <groupId>com.telnyx.sdk</groupId> |
| 11 | <artifactId>telnyx</artifactId> |
| 12 | <version>6.36.0</version> |
| 13 | </dependency> |
| 14 | |
| 15 | // Gradle |
| 16 | implementation("com.telnyx.sdk:telnyx:6.36.0") |
| 17 | ``` |
| 18 | |
| 19 | ## Setup |
| 20 | |
| 21 | ```java |
| 22 | import com.telnyx.sdk.client.TelnyxClient; |
| 23 | import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient; |
| 24 | |
| 25 | TelnyxClient client = TelnyxOkHttpClient.fromEnv(); |
| 26 | ``` |
| 27 | |
| 28 | All examples below assume `client` is already initialized as shown above. |
| 29 | |
| 30 | ## Error Handling |
| 31 | |
| 32 | All API calls can fail with network errors, rate limits (429), validation errors (422), |
| 33 | or authentication errors (401). Always handle errors in production code: |
| 34 | |
| 35 | ```java |
| 36 | import com.telnyx.sdk.errors.TelnyxServiceException; |
| 37 | |
| 38 | try { |
| 39 | var result = client.messages().send(params); |
| 40 | } catch (TelnyxServiceException e) { |
| 41 | System.err.println("API error " + e.statusCode() + ": " + e.getMessage()); |
| 42 | if (e.statusCode() == 422) { |
| 43 | System.err.println("Validation error — check required fields and formats"); |
| 44 | } else if (e.statusCode() == 429) { |
| 45 | // Rate limited — wait and retry with exponential backoff |
| 46 | Thread.sleep(1000); |
| 47 | } |
| 48 | } |
| 49 | ``` |
| 50 | |
| 51 | Common error codes: `401` invalid API key, `403` insufficient permissions, |
| 52 | `404` resource not found, `422` validation error (check field formats), |
| 53 | `429` rate limited (retry with exponential backoff). |
| 54 | |
| 55 | ## Important Notes |
| 56 | |
| 57 | - **Pagination:** List methods return a page. Use `.autoPager()` for automatic iteration: `for (var item : page.autoPager()) { ... }`. For manual control, use `.hasNextPage()` and `.nextPage()`. |
| 58 | |
| 59 | ## Transcribe speech to text |
| 60 | |
| 61 | 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. |
| 62 | |
| 63 | `POST /ai/audio/transcriptions` |
| 64 | |
| 65 | ```java |
| 66 | import com.telnyx.sdk.models.ai.audio.AudioTranscribeParams; |
| 67 | import com.telnyx.sdk.models.ai.audio.AudioTranscribeResponse; |
| 68 | |
| 69 | AudioTranscribeParams params = AudioTranscribeParams.builder() |
| 70 | .model(AudioTranscribeParams.Model.DISTIL_WHISPER_DISTIL_LARGE_V2) |
| 71 | .build(); |
| 72 | AudioTranscribeResponse response = client.ai().audio().transcribe(params); |
| 73 | ``` |
| 74 | |
| 75 | Returns: `duration` (number), `segments` (array[object]), `text` (string) |
| 76 | |
| 77 | ## Create a chat completion |
| 78 | |
| 79 | 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. |
| 80 | |
| 81 | `POST /ai/chat/completions` — Required: `messages` |
| 82 | |
| 83 | 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) |
| 84 | |
| 85 | ```java |
| 86 | import com.telnyx.sdk.models.ai.chat.ChatCreateCompletionParams; |
| 87 | import com.telnyx.sdk.models.ai.chat.ChatCreateCompletionResponse; |
| 88 | |
| 89 | ChatCreateCompletionParams params = ChatCreateCompletionParams.builder() |
| 90 | .addMessage(ChatCreateCompletionParams.Message.builder() |
| 91 | .content("You are a friendly chatbot.") |
| 92 | .role(ChatCreateCompletionParams.Message.Role.SYSTEM) |
| 93 | .build()) |
| 94 | .addMessage(ChatCreateCompletionParams.Message.builder() |
| 95 | .content("Hello, world!") |
| 96 | .role(ChatCreateCompletionParams.Message.Role.USER) |
| 97 | .build()) |
| 98 | .build(); |
| 99 | ChatCreateCompletionResponse response = client.ai().chat().createCompletion(params); |
| 100 | ``` |
| 101 | |
| 102 | ## List conversations |
| 103 | |
| 104 | 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. |
| 105 | |
| 106 | `GET /ai/conversations` |
| 107 | |
| 108 | ```java |
| 109 | import com.telnyx.sdk.models.ai.conversations.ConversationListParams; |
| 110 | import com.telnyx.sdk.models.ai.conversations.ConversationListResponse; |
| 111 | |
| 112 | ConversationListResponse conversations = client.ai().conversations().list(); |
| 113 | ``` |
| 114 | |
| 115 | Returns: `created_at` (date-time), `id` (uuid), `last_message_at` (date-time), `metadata` (object), `name` (string) |
| 116 | |
| 117 | ## Create a conversation |
| 118 | |
| 119 | Create a new AI Conve |