Skip to content

Commit 2c1993b

Browse files
committed
fix formatting
1 parent 69ffa9e commit 2c1993b

File tree

1 file changed

+100
-104
lines changed

1 file changed

+100
-104
lines changed

model-router.mdx

Lines changed: 100 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -248,125 +248,125 @@ callOpenAI()
248248
package main
249249

250250
import (
251-
"bytes"
252-
"encoding/json"
253-
"fmt"
254-
"io"
255-
"net/http"
256-
"os"
251+
"bytes"
252+
"encoding/json"
253+
"fmt"
254+
"io"
255+
"net/http"
256+
"os"
257257
)
258258

259259
// Define structs for the request and response
260260
type Message struct {
261-
Role string `json:"role"`
262-
Content string `json:"content"`
261+
Role string `json:"role"`
262+
Content string `json:"content"`
263263
}
264264

265265
type ChatCompletionRequest struct {
266-
Model string `json:"model"`
267-
Messages []Message `json:"messages"`
268-
MaxTokens int `json:"max_tokens"`
269-
Temperature float64 `json:"temperature"`
266+
Model string `json:"model"`
267+
Messages []Message `json:"messages"`
268+
MaxTokens int `json:"max_tokens"`
269+
Temperature float64 `json:"temperature"`
270270
}
271271

272272
type ChatCompletionChoice struct {
273-
Message Message `json:"message"`
274-
Index int `json:"index"`
275-
FinishReason string `json:"finish_reason"`
273+
Message Message `json:"message"`
274+
Index int `json:"index"`
275+
FinishReason string `json:"finish_reason"`
276276
}
277277

278278
type Usage struct {
279-
PromptTokens int `json:"prompt_tokens"`
280-
CompletionTokens int `json:"completion_tokens"`
281-
TotalTokens int `json:"total_tokens"`
279+
PromptTokens int `json:"prompt_tokens"`
280+
CompletionTokens int `json:"completion_tokens"`
281+
TotalTokens int `json:"total_tokens"`
282282
}
283283

284284
type ChatCompletionResponse struct {
285-
ID string `json:"id"`
286-
Object string `json:"object"`
287-
Created int64 `json:"created"`
288-
Model string `json:"model"`
289-
Choices []ChatCompletionChoice `json:"choices"`
290-
Usage Usage `json:"usage"`
285+
ID string `json:"id"`
286+
Object string `json:"object"`
287+
Created int64 `json:"created"`
288+
Model string `json:"model"`
289+
Choices []ChatCompletionChoice `json:"choices"`
290+
Usage Usage `json:"usage"`
291291
}
292292

293293
func main() {
294-
// Your Hypermode API key
295-
apiKey := "<HYPERMODE_API_KEY>"
296-
297-
// Use the Hypermode Model Router base url
298-
baseURL := "https://models.hypermode.host"
299-
300-
// API endpoint
301-
endpoint := baseURL + "/v1/chat/completions"
302-
303-
// Create the request payload
304-
requestBody := ChatCompletionRequest{
305-
Model: "meta-llama/llama-4-scout-17b-16e-instruct",
306-
Messages: []Message{
307-
{
308-
Role: "system",
309-
Content: "You are a helpful assistant.",
310-
},
311-
{
312-
Role: "user",
313-
Content: "What is Dgraph?",
314-
},
315-
},
316-
MaxTokens: 150,
317-
Temperature: 0.7,
318-
}
319-
320-
// Convert the request to JSON
321-
jsonData, err := json.Marshal(requestBody)
322-
if err != nil {
323-
fmt.Printf("Error marshaling JSON: %v\n", err)
324-
os.Exit(1)
325-
}
326-
327-
// Create an HTTP request
328-
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonData))
329-
if err != nil {
330-
fmt.Printf("Error creating request: %v\n", err)
331-
os.Exit(1)
332-
}
333-
334-
// Set headers
335-
req.Header.Set("Content-Type", "application/json")
336-
req.Header.Set("Authorization", "Bearer "+apiKey)
337-
338-
// Create an HTTP client and send the request
339-
client := &http.Client{}
340-
resp, err := client.Do(req)
341-
if err != nil {
342-
fmt.Printf("Error sending request: %v\n", err)
343-
os.Exit(1)
344-
}
345-
defer resp.Body.Close()
346-
347-
// Read the response body
348-
body, err := io.ReadAll(resp.Body)
349-
if err != nil {
350-
fmt.Printf("Error reading response: %v\n", err)
351-
os.Exit(1)
352-
}
353-
354-
// Check if the request was successful
355-
if resp.StatusCode == http.StatusOK {
356-
// Parse and print the response
357-
var response ChatCompletionResponse
358-
err = json.Unmarshal(body, &response)
359-
if err != nil {
360-
fmt.Printf("Error parsing response: %v\n", err)
361-
os.Exit(1)
362-
}
363-
364-
fmt.Println(response.Choices[0].Message.Content)
365-
} else {
366-
// Print error information
367-
fmt.Printf("Error: %d\n", resp.StatusCode)
368-
fmt.Println(string(body))
369-
}
294+
// Your Hypermode API key
295+
apiKey := "<HYPERMODE_API_KEY>"
296+
297+
// Use the Hypermode Model Router base url
298+
baseURL := "https://models.hypermode.host"
299+
300+
// API endpoint
301+
endpoint := baseURL + "/v1/chat/completions"
302+
303+
// Create the request payload
304+
requestBody := ChatCompletionRequest{
305+
Model: "meta-llama/llama-4-scout-17b-16e-instruct",
306+
Messages: []Message{
307+
{
308+
Role: "system",
309+
Content: "You are a helpful assistant.",
310+
},
311+
{
312+
Role: "user",
313+
Content: "What is Dgraph?",
314+
},
315+
},
316+
MaxTokens: 150,
317+
Temperature: 0.7,
318+
}
319+
320+
// Convert the request to JSON
321+
jsonData, err := json.Marshal(requestBody)
322+
if err != nil {
323+
fmt.Printf("Error marshaling JSON: %v\n", err)
324+
os.Exit(1)
325+
}
326+
327+
// Create an HTTP request
328+
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonData))
329+
if err != nil {
330+
fmt.Printf("Error creating request: %v\n", err)
331+
os.Exit(1)
332+
}
333+
334+
// Set headers
335+
req.Header.Set("Content-Type", "application/json")
336+
req.Header.Set("Authorization", "Bearer "+apiKey)
337+
338+
// Create an HTTP client and send the request
339+
client := &http.Client{}
340+
resp, err := client.Do(req)
341+
if err != nil {
342+
fmt.Printf("Error sending request: %v\n", err)
343+
os.Exit(1)
344+
}
345+
defer resp.Body.Close()
346+
347+
// Read the response body
348+
body, err := io.ReadAll(resp.Body)
349+
if err != nil {
350+
fmt.Printf("Error reading response: %v\n", err)
351+
os.Exit(1)
352+
}
353+
354+
// Check if the request was successful
355+
if resp.StatusCode == http.StatusOK {
356+
// Parse and print the response
357+
var response ChatCompletionResponse
358+
err = json.Unmarshal(body, &response)
359+
if err != nil {
360+
fmt.Printf("Error parsing response: %v\n", err)
361+
os.Exit(1)
362+
}
363+
364+
fmt.Println(response.Choices[0].Message.Content)
365+
} else {
366+
// Print error information
367+
fmt.Printf("Error: %d\n", resp.StatusCode)
368+
fmt.Println(string(body))
369+
}
370370
}
371371

372372
```
@@ -497,7 +497,3 @@ Hypermode Model Router simplifies how you pay for model consumption, billing by
497497
model compute time and type. Model Units are included with all paid plans and
498498
are calculated by taking the Model Unit Multiplier times the seconds of model
499499
runtime across all requests.
500-
501-
```
502-
503-
```

0 commit comments

Comments
 (0)