-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai.sh
45 lines (36 loc) · 1.07 KB
/
openai.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
```bash
#!/bin/bash
# OpenAI API key
API_KEY="your-api-key-here"
# Base URL for OpenAI Assistant API
BASE_URL="https://api.openai.com/v1"
# Function to create headers
function create_headers() {
echo "-H 'Content-Type: application/json' \
-H 'Authorization: Bearer $API_KEY'"
}
# Function to generate a completion
function generate_completion() {
local model="$1"
local prompt="$2"
local max_tokens="$3"
local data=$(jq -n \
--arg prompt "$prompt" \
--arg model "$model" \
--argjson max_tokens "$max_tokens" \
'{
model: $model,
prompt: $prompt,
max_tokens: $max_tokens
}')
curl -X POST "$BASE_URL/completions" \
$(create_headers) \
-d "$data"
}
# Example usage of generate_completion
function example_usage() {
local model="gpt-4-turbo"
local prompt="Translate the following English text to French: 'Hello, how are you?'"
local max_tokens=60
generate_completion "$model" "$prompt" "$max_tokens"
}