Skip to content

Commit

Permalink
refactor: changed cors configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianmusial committed Mar 18, 2024
1 parent 4397ed6 commit 48914e3
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 34 deletions.
7 changes: 5 additions & 2 deletions apps/api/src/app/chat/agents/currency/currency.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ export class CurrencyService {
.get('https://api.frankfurter.app/latest', { params })
.pipe(
catchError((error: AxiosError) => {
this.logger.error(error.response.data);
throw new HttpException(error.response.data, error.response.status);
const message = error?.response?.data || {
message: 'Unknown error',
};
this.logger.error(message);
throw new HttpException(message, error?.response?.status || 500);
}),
),
);
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/app/chat/agents/currency/get-currency.agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CurrencyService } from './currency.service';

@Injectable()
export class GetCurrencyAgent extends AgentBase {
definition: AssistantCreateParams.AssistantToolsFunction = {
override definition: AssistantCreateParams.AssistantToolsFunction = {
type: 'function',
function: {
name: this.constructor.name,
Expand All @@ -24,13 +24,13 @@ export class GetCurrencyAgent extends AgentBase {
};

constructor(
protected readonly agentService: AgentService,
override readonly agentService: AgentService,
private readonly currencyService: CurrencyService,
) {
super(agentService);
}

async output(data: AgentData): Promise<string> {
override async output(data: AgentData): Promise<string> {
try {
// Parse the parameters from the input data
const params = JSON.parse(data.params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PokemonService } from './pokemon.service';

@Injectable()
export class GetPokemonListAgent extends AgentBase {
definition: AssistantCreateParams.AssistantToolsFunction = {
override definition: AssistantCreateParams.AssistantToolsFunction = {
type: 'function',
function: {
name: this.constructor.name,
Expand All @@ -19,13 +19,13 @@ export class GetPokemonListAgent extends AgentBase {
};

constructor(
protected readonly agentService: AgentService,
override readonly agentService: AgentService,
private readonly pokemonService: PokemonService,
) {
super(agentService);
}

async output(): Promise<string> {
override async output(): Promise<string> {
try {
// Get the list of Pokemon
const pokemon = await this.pokemonService.getPokemonList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PokemonService } from './pokemon.service';

@Injectable()
export class GetPokemonStatsAgent extends AgentBase {
definition: AssistantCreateParams.AssistantToolsFunction = {
override definition: AssistantCreateParams.AssistantToolsFunction = {
type: 'function',
function: {
name: this.constructor.name,
Expand All @@ -25,13 +25,13 @@ export class GetPokemonStatsAgent extends AgentBase {
};

constructor(
protected readonly agentService: AgentService,
override readonly agentService: AgentService,
private readonly pokemonService: PokemonService,
) {
super(agentService);
}

async output(data: AgentData): Promise<string> {
override async output(data: AgentData): Promise<string> {
try {
// Parse the parameters from the input data
const params = JSON.parse(data.params);
Expand Down
10 changes: 6 additions & 4 deletions apps/api/src/app/chat/agents/pokemon/pokemon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export class PokemonService {
const { data } = await firstValueFrom(
this.httpService.get(`${this.apiUrl}/?limit=30`).pipe(
catchError((error: AxiosError) => {
this.logger.error(error.response.data);
throw new HttpException(error.response.data, error.response.status);
const message = error?.response?.data || { message: 'Unknown error' };
this.logger.error(message);
throw new HttpException(message, error?.response?.status || 500);
}),
),
);
Expand All @@ -27,8 +28,9 @@ export class PokemonService {
const { data } = await firstValueFrom(
this.httpService.get(`${this.apiUrl}/${name?.toLowerCase()}`).pipe(
catchError((error: AxiosError) => {
this.logger.error(error.response.data);
throw new HttpException(error.response.data, error.response.status);
const message = error?.response?.data || { message: 'Unknown error' };
this.logger.error(message);
throw new HttpException(message, error?.response?.status || 500);
}),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WeatherService } from './weather.service';

@Injectable()
export class GetCurrentWeatherAgent extends AgentBase {
definition: AssistantCreateParams.AssistantToolsFunction = {
override definition: AssistantCreateParams.AssistantToolsFunction = {
type: 'function',
function: {
name: this.constructor.name,
Expand All @@ -25,13 +25,13 @@ export class GetCurrentWeatherAgent extends AgentBase {
};

constructor(
protected readonly agentService: AgentService,
override readonly agentService: AgentService,
private readonly weatherService: WeatherService,
) {
super(agentService);
}

async output(data: AgentData): Promise<string> {
override async output(data: AgentData): Promise<string> {
try {
// Parse the parameters from the input data
const params = JSON.parse(data.params);
Expand Down
7 changes: 5 additions & 2 deletions apps/api/src/app/chat/agents/weather/weather.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ export class WeatherService {
.get('https://api.openweathermap.org/data/2.5/weather', { params })
.pipe(
catchError((error: AxiosError) => {
this.logger.error(error.response.data);
throw new HttpException(error.response.data, error.response.status);
const message = error?.response?.data || {
message: 'Unknown error',
};
this.logger.error(message);
throw new HttpException(message, error?.response?.status || 500);
}),
),
);
Expand Down
6 changes: 6 additions & 0 deletions apps/api/src/app/cors.helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const isDevelopment = process.env['NODE_ENV'] === 'development';
export const cors = {
origin: isDevelopment ? '*' : true,
methods: ['GET', 'POST'],
credentials: true,
};
11 changes: 5 additions & 6 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

import { AppModule } from './app/app.module';
import { cors } from './app/cors.helpers';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
Expand All @@ -12,15 +12,14 @@ async function bootstrap() {
.setVersion('0.1.0')
.build();
const document = SwaggerModule.createDocument(app, config);

SwaggerModule.setup('api/docs', app, document);

app.setGlobalPrefix(globalPrefix);
app.enableCors({
origin: '*',
credentials: true,
});

const port = process.env.PORT || 3000;
app.enableCors(cors);

const port = process.env['PORT'] || 3000;
await app.listen(port);

Logger.log(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<section>
A NestJS library for building efficient, scalable, and quick solutions based on the OpenAI Assistant API (chatbots) 🤖 🚀
A NestJS library for building efficient, scalable, and quick solutions based
on the OpenAI Assistant API (chatbots) 🤖 🚀
</section>

<section class="chat-home__examples">
The following are some examples of what you can do with the demo:

<ul class="chat-home__list">
<li>
Speak about the weather (eg. What's the weather in London?)
</li>
<li>Speak about the weather (eg. What's the weather in London?)</li>
<li>
Speak about the exchange rate (eg. What's the exchange rate for USD?)
</li>
<li>
Speak about the Pokemon (eg. Show me the stats of Pikachu)
</li>
<li>Speak about the Pokemon (eg. Show me the stats of Pikachu)</li>
</ul>
</section>
2 changes: 1 addition & 1 deletion libs/openai-assistant/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@boldare/openai-assistant",
"description": "NestJS library for building chatbot solutions based on the OpenAI Assistant API",
"version": "0.0.1-dev.3",
"version": "0.0.1-dev.4",
"private": false,
"dependencies": {
"tslib": "^2.3.0",
Expand Down

0 comments on commit 48914e3

Please sign in to comment.