chatbot with gpt3 in angular

169 Views Asked by At

I'm making my own chat application in Angular with chatgpt3. When I tried the api url in the documentation, I got error 429. What do you think could be the reason?

service.ts

export class OpenaiService {

  private apiKey = 'sk-xxxx';
  private apiUrl = 'https://api.openai.com/v1/chat/completions';

  constructor(private http: HttpClient) { }

  teachGPT(userMessage: string): Observable<string> {
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${this.apiKey}`
    });

    const requestData = {
      model: 'gpt-3.5-turbo',
      messages: [
        { role: 'system', content: 'You are a helpful assistant.' },
        { role: 'user', content: userMessage }
      ]
    };

    return this.http.post<any>(this.apiUrl, requestData, { headers })
      .pipe(
        map(response => response.choices[0].message.content),
        catchError(error => {
          console.error('An error occurred:', error);
          return of('');
        })
      );
  }
}

chat.ts

export class ChatPageComponent {

  userMessage: string = '';
  chatGPTResponse: string | undefined;

  constructor(private openaiService: OpenaiService) { }

  onSubmit() {
    this.openaiService.teachGPT(this.userMessage)
      .subscribe(
        response => {
          this.chatGPTResponse = response;
          console.log('ChatGPT Cevabı:', response);
        },
        error => {
          console.error('Bir hata oluştu:', error);
        }
      );
  }
}

console error: enter image description here

When I use the URL https://api.openai.com/v1/chat/completions, which is the url in the gpt 3.5 turbo documentation, I get error 429. If the URL suggested by gpt was https://api.openai.com/v1/engines/davinci-codex/completions, I got a 400 bad request error.

1

There are 1 best solutions below

0
On

I found the answer to the last one. gpt3 was giving credit to new members for the first 3 months for api trials. When I opened a new account and tried it, the problem was solved.