-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: examples of agents (function calling), knowledge base (#44)
* feat: demo page with navbar * refactor: renamed package name ai-assistant -> openai-assistant * feat: examples of agents (function calling) * refactor: changed cors configuration * refactor: websocket configuration * feat: added document to the knowledge base * chore: release version 1.0.0
- Loading branch information
1 parent
51efa7a
commit ce5efb2
Showing
29 changed files
with
551 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# OpenAI API Key | ||
OPENAI_API_KEY= | ||
|
||
# Assistant ID - leave it empty if you don't have an assistant yet | ||
ASSISTANT_ID= | ||
|
||
# Agents: | ||
# ------------------------------------------------------------------- | ||
# OpenWeather (Current Weather Data) | ||
OPENWEATHER_API_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
name: Demo deploy | ||
|
||
on: | ||
push: | ||
branches: | ||
- preview | ||
release: | ||
types: [published] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GetAnimalAgent } from './get-animal.agent'; | ||
import { AgentModule } from '@boldare/openai-assistant'; | ||
import { WeatherModule } from './weather/weather.module'; | ||
import { PokemonModule } from './pokemon/pokemon.module'; | ||
import { CurrencyModule } from './currency/currency.module'; | ||
|
||
@Module({ | ||
imports: [AgentModule], | ||
providers: [GetAnimalAgent], | ||
imports: [WeatherModule, PokemonModule, CurrencyModule], | ||
}) | ||
export class AgentsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GetCurrencyAgent } from './get-currency.agent'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { CurrencyService } from './currency.service'; | ||
import { AgentModule } from '@boldare/openai-assistant'; | ||
|
||
@Module({ | ||
imports: [AgentModule, HttpModule], | ||
providers: [CurrencyService, GetCurrencyAgent], | ||
}) | ||
export class CurrencyModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { HttpException, Injectable, Logger } from '@nestjs/common'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { catchError, firstValueFrom } from 'rxjs'; | ||
import { AxiosError } from 'axios'; | ||
|
||
@Injectable() | ||
export class CurrencyService { | ||
private readonly logger = new Logger(CurrencyService.name); | ||
|
||
constructor(private httpService: HttpService) {} | ||
|
||
async getExchangeRate(currency: string) { | ||
const params = { from: currency }; | ||
const { data } = await firstValueFrom( | ||
this.httpService | ||
.get('https://api.frankfurter.app/latest', { params }) | ||
.pipe( | ||
catchError((error: AxiosError) => { | ||
const message = error?.response?.data || { | ||
message: 'Unknown error', | ||
}; | ||
this.logger.error(message); | ||
throw new HttpException(message, error?.response?.status || 500); | ||
}), | ||
), | ||
); | ||
|
||
return data; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
apps/api/src/app/chat/agents/currency/get-currency.agent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AssistantCreateParams } from 'openai/resources/beta'; | ||
import { AgentBase, AgentData, AgentService } from '@boldare/openai-assistant'; | ||
import { CurrencyService } from './currency.service'; | ||
|
||
@Injectable() | ||
export class GetCurrencyAgent extends AgentBase { | ||
override definition: AssistantCreateParams.AssistantToolsFunction = { | ||
type: 'function', | ||
function: { | ||
name: this.constructor.name, | ||
description: 'Get the current currency exchange rate.', | ||
parameters: { | ||
type: 'object', | ||
properties: { | ||
currency: { | ||
type: 'string', | ||
description: 'Currency code e.g. USD, EUR, GBP, etc.', | ||
}, | ||
}, | ||
required: ['currency'], | ||
}, | ||
}, | ||
}; | ||
|
||
constructor( | ||
override readonly agentService: AgentService, | ||
private readonly currencyService: CurrencyService, | ||
) { | ||
super(agentService); | ||
} | ||
|
||
override async output(data: AgentData): Promise<string> { | ||
try { | ||
// Parse the parameters from the input data | ||
const params = JSON.parse(data.params); | ||
const currency = params?.currency; | ||
|
||
// Check if the currency is provided | ||
if (!currency) { | ||
return 'No currency provided'; | ||
} | ||
|
||
// Get the current currency exchange rate | ||
const response = await this.currencyService.getExchangeRate(currency); | ||
|
||
// Return the result | ||
return `The current exchange rate for ${currency} is: ${JSON.stringify( | ||
response, | ||
)}`; | ||
} catch (errors) { | ||
// Handle the errors | ||
return `Invalid data: ${JSON.stringify(errors)}`; | ||
} | ||
} | ||
} |
Oops, something went wrong.