Here's my environment.prod.ts
export const OPENAI_API_KEY = "sk-XXXXXXXXXXXX"
Here's my service.ts file
import { Injectable } from '@angular/core'; import { Configuration, OpenAIApi } from 'openai'; import { filter, from, map } from 'rxjs'; import { environment, OPENAI_API_KEY } from 'src/environments/environment';
@Injectable({ providedIn: 'root' })
export class OpenAiService {
constructor(){}
readonly configuration = new Configuration({
apiKey: OPENAI_API_KEY });
readonly openai = new OpenAIApi(this.configuration);
async getDataFromOpenAPI(text: string) { const completion = await this.openai.createCompletion({ model: "text-davinci-002", prompt: text, }); console.log(completion.data.choices[0].text); } getDataFromOpenAI(text: string) { from(this.openai.createCompletion({ model: "text-davinci-003", prompt: text, max_tokens: 256 })).pipe( filter((resp: { data: any; }) => !!resp && !!resp.data), map(resp => resp.data), filter((data: any) => data.choices && data.choices.length > 0 && data.choices[0].text), map(data => data.choices[0].text) ).subscribe(data => { console.log(data); }); }
}
I've read through this several times and there are some gaps.
This line "apiKey: environment.OPENAI_API_KEY" throws a "Property 'OPENAI_API_KEY' does not exist on type" ...... Error. I am searching for a resolution and have been unsuucessful.
I copied and pasted your "rxjs" code. It needs the import statement..which was not mentioned. "import { filter, from, map } from 'rxjs'";
Gordon Thomas Cumming
When I npm install chatgpt-api
But if I import { ChatGPT } from 'chatgpt-api';
It complains - What am I doing wrong