$npx -y skills add team-telnyx/ai --skill telnyx-ai-inference-goAccess Telnyx LLM inference APIs, embeddings, and AI analytics for call insights and summaries. This skill provides Go SDK examples.
| 1 | <!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. --> |
| 2 | |
| 3 | # Telnyx Ai Inference - Go |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | go get github.com/team-telnyx/telnyx-go |
| 9 | ``` |
| 10 | |
| 11 | ## Setup |
| 12 | |
| 13 | ```go |
| 14 | import ( |
| 15 | "context" |
| 16 | "fmt" |
| 17 | "os" |
| 18 | |
| 19 | "github.com/team-telnyx/telnyx-go" |
| 20 | "github.com/team-telnyx/telnyx-go/option" |
| 21 | ) |
| 22 | |
| 23 | client := telnyx.NewClient( |
| 24 | option.WithAPIKey(os.Getenv("TELNYX_API_KEY")), |
| 25 | ) |
| 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 | ```go |
| 36 | import "errors" |
| 37 | |
| 38 | result, err := client.Messages.Send(ctx, params) |
| 39 | if err != nil { |
| 40 | var apiErr *telnyx.Error |
| 41 | if errors.As(err, &apiErr) { |
| 42 | switch apiErr.StatusCode { |
| 43 | case 422: |
| 44 | fmt.Println("Validation error — check required fields and formats") |
| 45 | case 429: |
| 46 | // Rate limited — wait and retry with exponential backoff |
| 47 | fmt.Println("Rate limited, retrying...") |
| 48 | default: |
| 49 | fmt.Printf("API error %d: %s\n", apiErr.StatusCode, apiErr.Error()) |
| 50 | } |
| 51 | } else { |
| 52 | fmt.Println("Network error — check connectivity and retry") |
| 53 | } |
| 54 | } |
| 55 | ``` |
| 56 | |
| 57 | Common error codes: `401` invalid API key, `403` insufficient permissions, |
| 58 | `404` resource not found, `422` validation error (check field formats), |
| 59 | `429` rate limited (retry with exponential backoff). |
| 60 | |
| 61 | ## Important Notes |
| 62 | |
| 63 | - **Pagination:** Use `ListAutoPaging()` for automatic iteration: `iter := client.Resource.ListAutoPaging(ctx, params); for iter.Next() { item := iter.Current() }`. |
| 64 | |
| 65 | ## Transcribe speech to text |
| 66 | |
| 67 | 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. |
| 68 | |
| 69 | `POST /ai/audio/transcriptions` |
| 70 | |
| 71 | ```go |
| 72 | response, err := client.AI.Audio.Transcribe(context.Background(), telnyx.AIAudioTranscribeParams{ |
| 73 | Model: telnyx.AIAudioTranscribeParamsModelDistilWhisperDistilLargeV2, |
| 74 | }) |
| 75 | if err != nil { |
| 76 | log.Fatal(err) |
| 77 | } |
| 78 | fmt.Printf("%+v\n", response.Text) |
| 79 | ``` |
| 80 | |
| 81 | Returns: `duration` (number), `segments` (array[object]), `text` (string) |
| 82 | |
| 83 | ## Create a chat completion |
| 84 | |
| 85 | 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. |
| 86 | |
| 87 | `POST /ai/chat/completions` — Required: `messages` |
| 88 | |
| 89 | 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) |
| 90 | |
| 91 | ```go |
| 92 | response, err := client.AI.Chat.NewCompletion(context.Background(), telnyx.AIChatNewCompletionParams{ |
| 93 | Messages: []telnyx.AIChatNewCompletionParamsMessage{{ |
| 94 | Role: "system", |
| 95 | Content: telnyx.AIChatNewCompletionParamsMessageContentUnion{ |
| 96 | OfString: telnyx.String("You are a friendly chatbot."), |
| 97 | }, |
| 98 | }, { |
| 99 | Role: "user", |
| 100 | Content: telnyx.AIChatNewCompletionParamsMessageContentUnion{ |
| 101 | OfString: telnyx.String("Hello, world!"), |
| 102 | }, |
| 103 | }}, |
| 104 | }) |
| 105 | if err != nil { |
| 106 | log.Fatal(err) |
| 107 | } |
| 108 | fmt.Printf("%+v\n", response) |
| 109 | ``` |
| 110 | |
| 111 | ## List conversations |
| 112 | |
| 113 | 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. |
| 114 | |
| 115 | `GET /ai/conversations` |
| 116 | |
| 117 | ```go |
| 118 | conversations, err := client.AI.Conversations.List(context.Background(), telnyx.AIConversationListParams{}) |
| 119 | if err != nil { |
| 120 | log.Fatal(err) |
| 121 | } |
| 122 | fmt.Printf("%+v\n", conversations.Data) |
| 123 | ``` |
| 124 | |
| 125 | Returns: `created_at` (date-time), `id` (uuid), `last_message_at` (date-time), `metadata` (object), `name` (string) |
| 126 | |
| 127 | ## Create a conversation |
| 128 | |
| 129 | Create a new AI Conversation. |
| 130 | |
| 131 | `POST /ai/conversations` |
| 132 | |
| 133 | Optional: `metadata` (object), `name` (string) |
| 134 | |
| 135 | ```go |
| 136 | conversation, err := client.AI.Conversations.New(context.Background(), telnyx.AIConversationNewParams{}) |
| 137 | if err != nil { |
| 138 | log.Fatal(err) |
| 139 | } |
| 140 | fmt.Printf("%+v\n", conversation.ID) |
| 141 | ``` |
| 142 | |
| 143 | Returns: `created_at` (date-time), `id` (uuid), `last_message_at` (date-time), `metad |