added an optional api key. see #144
This commit is contained in:
parent
57c59914e3
commit
1730922c04
1 changed files with 15 additions and 7 deletions
|
|
@ -23,21 +23,26 @@ export class OpenAIComp {
|
||||||
|
|
||||||
host = '';
|
host = '';
|
||||||
model = '';
|
model = '';
|
||||||
|
apiKey = '';
|
||||||
stream = false;
|
stream = false;
|
||||||
|
|
||||||
constructor(host, model, stream = false) {
|
constructor(host, model, apiKey = '', stream = false) {
|
||||||
this.host = host.trim().replace(/\/+$/, "");
|
this.host = host.trim().replace(/\/+$/, "");
|
||||||
this.model = model;
|
this.model = model;
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
|
this.apiKey = apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fetchModels = async () => {
|
fetchModels = async () => {
|
||||||
|
const curr_headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
};
|
||||||
|
if(this.apiKey !== '') curr_headers["Authorization"] = "Bearer "+ this.apiKey;
|
||||||
|
|
||||||
const response = await fetch(this.host + "/v1/models", {
|
const response = await fetch(this.host + "/v1/models", {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: curr_headers,
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|
@ -59,12 +64,15 @@ export class OpenAIComp {
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchResponse = async (messages, maxTokens = 0) => {
|
fetchResponse = async (messages, maxTokens = 0) => {
|
||||||
|
const curr_headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
};
|
||||||
|
if(this.apiKey !== '') curr_headers["Authorization"] = "Bearer "+ this.apiKey;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(this.host + "/v1/chat/completions", {
|
const response = await fetch(this.host + "/v1/chat/completions", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: curr_headers,
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
model: this.model,
|
model: this.model,
|
||||||
messages: messages,
|
messages: messages,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue