-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChatGptWidget.js
More file actions
128 lines (122 loc) · 3.49 KB
/
Copy pathChatGptWidget.js
File metadata and controls
128 lines (122 loc) · 3.49 KB
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
126
127
128
(function () {
let template = document.createElement("template");
template.innerHTML = `
<style>
:host {}
/* Style for the container */
div {
margin: 50px auto;
max-width: 600px;
}
/* Style for the input container */
.input-container {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
/* Style for the input field */
#prompt-input {
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
width: 70%;
}
/* Style for the button */
#generate-button {
padding: 10px;
font-size: 16px;
background-color: #3cb6a9;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
width: 25%;
}
/* Style for the generated text area */
#generated-text {
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
width:96%;
}
</style>
<div>
<center>
<img src="https://1000logos.net/wp-content/uploads/2023/02/ChatGPT-Emblem.png" width="200"/>
<h1>ChatGPT</h1></center>
<div class="input-container">
<input type="text" id="prompt-input" placeholder="Enter a prompt">
<button id="generate-button">Generate Text</button>
</div>
<textarea id="generated-text" rows="10" cols="50" readonly></ textarea>
</div>
`;
class Widget extends HTMLElement {
constructor() {
super();
let shadowRoot = this.attachShadow({
mode: "open"
});
shadowRoot.appendChild(template.content.cloneNode(true));
this._props = {};
}
async connectedCallback() {
this.initMain();
}
async initMain() {
const generatedText = this.shadowRoot.getElementById("generated-text");
generatedText.value = "";
const {
apiKey
} = this._props || "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";
const {
max_tokens
} = this._props || 1024;
const generateButton = this.shadowRoot.getElementById("generate-button");
generateButton.addEventListener("click", async () => {
const promptInput = this.shadowRoot.getElementById("prompt-input");
const generatedText = this.shadowRoot.getElementById("generated-text");
generatedText.value = "Finding result...";
const prompt = promptInput.value;
const response = await fetch("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + apiKey
},
body: JSON.stringify({
"model": "text-davinci-002",
"prompt": prompt,
"max_tokens": parseInt(max_tokens),
"n": 1,
"temperature": 0.5
})
});
if (response.status === 200) {
const {
choices
} = await response.json();
const generatedTextValue = choices[0].text;
generatedText.value = generatedTextValue.replace(/^\n+/, '');
} else {
const error = await response.json();
alert("OpenAI Response: " + error.error.message);
generatedText.value = "";
}
});
}
onCustomWidgetBeforeUpdate(changedProperties) {
this._props = {
...this._props,
...changedProperties
};
}
onCustomWidgetAfterUpdate(changedProperties) {
this.initMain();
}
}
customElements.define("com-rohitchouhan-sap-chatgptwidget", Widget);
})();