Skip to content

Commit

Permalink
Update openai npm dependency to version 4.47.1
Browse files Browse the repository at this point in the history
  • Loading branch information
yagizhanNY committed May 23, 2024
1 parent 6d819a6 commit 346cffb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 67 deletions.
15 changes: 7 additions & 8 deletions src/app/chat/chat-content/chat-content.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import {
AfterViewChecked,
AfterViewInit,
Component,
ElementRef,
OnInit,
ViewChild,
ElementRef,
} from '@angular/core';

import { ApiKeyService } from 'src/app/services/api-key.service';
import { ChatService } from '../../services/chat.service';
import { MarkdownService } from 'ngx-markdown';
import { MatSnackBar } from '@angular/material/snack-bar';
import { ApiKeyService } from 'src/app/services/api-key.service';
import { ChatCompletionRequestMessage } from 'openai';

@Component({
selector: 'app-chat-content',
Expand All @@ -28,7 +28,7 @@ export class ChatContentComponent
) {}

@ViewChild('window') window!: any;
public messages: ChatCompletionRequestMessage[] = [];
public messages: any[] = [];
apiKey: string | null = '';
isBusy: boolean = false;
currChatSelected: string = '';
Expand Down Expand Up @@ -63,7 +63,7 @@ export class ChatContentComponent
return;
}
element.value = '';
const message: ChatCompletionRequestMessage = {
const message: any = {
role: 'user',
content: prompt,
};
Expand All @@ -74,12 +74,11 @@ export class ChatContentComponent
const completion = await this.chatService.createCompletionViaOpenAI(
this.messages
);
console.log(completion);
const completionMessage = this.markdownService.parse(
completion.data.choices[0].message?.content!
completion.choices[0].message?.content!
);

const responseMessage: ChatCompletionRequestMessage = {
const responseMessage: any = {
role: 'assistant',
content: completionMessage,
};
Expand Down
6 changes: 3 additions & 3 deletions src/app/services/chat-data.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { ChatCompletionMessage } from 'openai/resources';
import { ChatHistoryDetails } from '../shared/models/chat-history-details.model';
import { ChatCompletionRequestMessage } from 'openai';
import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root',
Expand All @@ -16,7 +16,7 @@ export class ChatDataService {

public setLocalStorageForSingleChat(
chatName: string,
chatData: ChatCompletionRequestMessage
chatData: ChatCompletionMessage
): void {
localStorage.setItem(`${chatName}`, JSON.stringify(chatData));
}
Expand Down
89 changes: 46 additions & 43 deletions src/app/services/chat.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Injectable } from '@angular/core';
import { ChatCompletionRequestMessage, Configuration, OpenAIApi } from 'openai';
import { BehaviorSubject, Observable } from 'rxjs';
import { ChatCompletion, ChatCompletionMessage, ChatCompletionMessageParam } from 'openai/resources';
import OpenAI, { ClientOptions } from 'openai';

import { ChatDataService } from './chat-data.service';
import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root',
})
export class ChatService {
openai!: OpenAIApi;
openai!: OpenAI;

messages: ChatCompletionRequestMessage[] = [];
private messagesSubject = new BehaviorSubject<ChatCompletionRequestMessage[]>(
messages: ChatCompletionMessage[] = [];
private messagesSubject = new BehaviorSubject<ChatCompletionMessage[]>(
[]
);

Expand All @@ -19,55 +21,56 @@ export class ChatService {
}

public updateConfiguration(): void {
const configuration = new Configuration({
const configuration: ClientOptions = {
apiKey: this.chatDataService.getAPIKeyFromLocalStore() ?? '',
});
dangerouslyAllowBrowser: true
};

this.openai = new OpenAIApi(configuration);
this.openai = new OpenAI(configuration);
}

async createCompletionViaOpenAI(messages: ChatCompletionRequestMessage[]) {
return await this.openai.createChatCompletion(
{
model: 'gpt-3.5-turbo',
messages: messages,
},
{
headers: {
'Content-Type': 'application/json',
'X-User-Agent': 'OpenAPI-Generator/1.0/Javascript',
},
}
);
async createCompletionViaOpenAI(messages: ChatCompletionMessageParam[]): Promise<ChatCompletion> {
return new Promise((resolve, reject) => {
this.openai.chat.completions.create(
{
model: 'gpt-4o',
messages: messages,
}
).then(message => {
resolve(message);
}).catch(err => {
reject(err);
})
})
}

async getTitleFromChatGpt(messages: ChatCompletionRequestMessage[]) {
return await this.openai.createChatCompletion(
{
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: `create a max 10 character title from below messages. ${JSON.stringify(
messages
)}`,
},
],
},
{
headers: {
'Content-Type': 'application/json',
'X-User-Agent': 'OpenAPI-Generator/1.0/Javascript',
},
}
);
async getTitleFromChatGpt(messages: ChatCompletionMessageParam[]): Promise<ChatCompletion> {
return new Promise((resolve, reject) => {
this.openai.chat.completions.create(
{
model: 'gpt-4o',
messages: [
{
role: 'user',
content: `create a max 10 character title from below messages. ${JSON.stringify(
messages
)}`,
},
],
}
).then(message => {
resolve(message)
}).catch(err => {
reject(err);
})
})
}

public setMessagesSubject(event: ChatCompletionRequestMessage[]) {
public setMessagesSubject(event: ChatCompletionMessage[]) {
this.messagesSubject.next(event);
}

public getMessagesSubject(): Observable<ChatCompletionRequestMessage[]> {
public getMessagesSubject(): Observable<ChatCompletionMessage[]> {
return this.messagesSubject.asObservable();
}
}
4 changes: 2 additions & 2 deletions src/app/shared/models/chat-history-details.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChatCompletionRequestMessage } from 'openai';
import { ChatCompletionMessage } from "openai/resources";

export interface ChatHistoryDetails {
id: string;
title: string;
messages: ChatCompletionRequestMessage[];
messages: ChatCompletionMessage[];
}
21 changes: 10 additions & 11 deletions src/app/sidebar/sidebar.component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Component, OnInit } from '@angular/core';
import { ChatDataService } from '../services/chat-data.service';
import { v4 as uuidv4 } from 'uuid';
import { ChatService } from '../services/chat.service';
import { Router } from '@angular/router';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { UserDialogComponent } from '../user-dialog/user-dialog.component';

import { ApiKeyService } from '../services/api-key.service';
import { ChatHistoryDetails } from '../shared/models/chat-history-details.model';
import { ChatCompletionMessage } from 'openai/resources';
import { ChatDataService } from '../services/chat-data.service';
import ChatHistories from '../shared/models/chat-histories.model';
import { ChatCompletionRequestMessage } from 'openai';
import { ChatHistoryDetails } from '../shared/models/chat-history-details.model';
import { ChatService } from '../services/chat.service';
import { UserDialogComponent } from '../user-dialog/user-dialog.component';
import { v4 as uuidv4 } from 'uuid';

@Component({
selector: 'app-sidebar',
templateUrl: './sidebar.component.html',
Expand All @@ -18,12 +19,11 @@ export class SidebarComponent implements OnInit {
constructor(
private chatDataService: ChatDataService,
private chatService: ChatService,
private router: Router,
private dialogModel: MatDialog,
private apiKeyService: ApiKeyService
) {}

messages: ChatCompletionRequestMessage[] = [];
messages: ChatCompletionMessage[] = [];
chatHistories: ChatHistories = {
chatHistoryDetails: [],
};
Expand All @@ -42,7 +42,7 @@ export class SidebarComponent implements OnInit {
if (this.isHistoricalChat === false) {
const chatHistoryId = uuidv4();
const title = (await this.chatService.getTitleFromChatGpt(this.messages))
.data.choices[0].message?.content!;
.choices[0].message?.content!;

const chatHistory: ChatHistoryDetails = {
id: chatHistoryId,
Expand Down Expand Up @@ -126,7 +126,6 @@ export class SidebarComponent implements OnInit {
const result = this.chatHistories.chatHistoryDetails.some(
(c) => c.id === id
);
console.log(result);
return result;
}
}

0 comments on commit 346cffb

Please sign in to comment.