-
Notifications
You must be signed in to change notification settings - Fork 1
/
useOpenAI.ts
125 lines (105 loc) · 4.1 KB
/
useOpenAI.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import axios from "axios"
import { configDotenv } from "dotenv"
configDotenv()
let OPENAI_INITIAL_PROMPT = process.env.OPENAI_INITIAL_PROMPT
if (OPENAI_INITIAL_PROMPT === undefined || OPENAI_INITIAL_PROMPT === null || OPENAI_INITIAL_PROMPT === "") {
OPENAI_INITIAL_PROMPT = `Generate a 200 character Post like one of these:
"New electronic waste from discarded gadgets makes up for 70% of all toxic waste. Let's consider recycling and upcycling our tech to create a healthier environment for us all."
"In the realm of technology, the first alarm clock could only ring at 4am! Created by Levi Hutchins in 1787, its sole purpose was to wake him for his pre-dawn job. Truly an early bird that transformed the way we start our days."
"The first computer mouse was made of wood! Created by Douglas Engelbart in 1964, it was a simple wooden shell with two metal wheels. It was later patented in 1970."
Do not make it a question, avoid using or featuring gender or politics, do not use phrases like "Did you know". The simple Post should include a single interesting fact about the following:`
}
const apiKey = process.env.OPENAI_KEY
const headers = {
"Authorization": `Bearer ${apiKey}`,
"Content-Type": "application/json",
}
export const generatePost = async (content: string) => {
if (apiKey === undefined || apiKey === null || apiKey === "") {
console.error("OpenAI API Key not found. Please add OPENAI_KEY to your .env to use this plugin.")
return null
}
const model = "gpt-4"
const endpoint = `https://api.openai.com/v1/chat/completions`
let prompt = `${OPENAI_INITIAL_PROMPT}\n\n${content}`
prompt = prompt.substring(0, 1024)
try {
let response: any
let content: string
response = await axios.post(endpoint, {
model,
temperature: 0.2,
max_tokens: 200,
top_p: 0.1,
messages: [{
"role": "user",
"content": prompt
}]
}, {
headers: {
"Authorization": `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
})
content = response.data.choices[0].message.content
if (content.length > 280) {
console.log('Shortening...')
response = await axios.post(endpoint, {
model,
messages: [{
"role": "user",
"content": `Condense this Post: ${content}`
}]
}, {
headers: {
"Authorization": `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
})
content = response.data.choices[0].message.content
}
return content
} catch
(error) {
console.error(`Error generating Post: ${error}`)
if (error.response) {
console.log(error.response.data)
console.log(error.response.status)
console.log(error.response.headers)
}
return null
}
}
export const generateImage = async (prompt: string) => {
// const endpoint = `https://api.openai.com/v1/images/generations`
//
// try {
// const response = await axios.post(endpoint, {
// prompt,
// n: 1,
// size: "1024x1024"
// }, {
// headers,
// })
//
// const data = response.data
// console.log(JSON.stringify(data, null, 2))
//
// const image = data.images[0]
// // save image to file
// // const imageBuffer = Buffer.from(image, "base64")
// // fs.writeFileSync("image.png", imageBuffer)
// console.log("Generated image:", image.data.url)
// return image
//
//
// } catch (error) {
// console.error(`Error generating image: ${error}`)
// if (error.response) {
// console.log(error.response.data)
// console.log(error.response.status)
// console.log(error.response.headers)
// }
// return null
// }
}