-
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.
- Loading branch information
1 parent
86be28f
commit b61dfe9
Showing
23 changed files
with
260 additions
and
49 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,2 +1,3 @@ | ||
OPENAI_API_KEY= | ||
ASSISTANT_ID= | ||
POKEMON_API_URL= |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,19 @@ | ||
import { OnModuleInit } from '@nestjs/common'; | ||
import { AssistantCreateParams } from 'openai/resources/beta'; | ||
import { AgentService } from './agent.service'; | ||
import { AgentData } from './agent.model'; | ||
|
||
export class AgentBase implements OnModuleInit { | ||
definition: AssistantCreateParams.AssistantToolsFunction; | ||
|
||
onModuleInit(): void { | ||
this.agentService.add(this.definition, this.output.bind(this)); | ||
} | ||
|
||
constructor(protected readonly agentService: AgentService) {} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async output(data: AgentData): Promise<string> { | ||
return ''; | ||
} | ||
} |
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,7 @@ | ||
export type Agent = (data: AgentData) => Promise<string>; | ||
export type Agents = Record<string, Agent>; | ||
|
||
export interface AgentData { | ||
threadId: string; | ||
params: string; | ||
} |
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,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AgentService } from './agent.service'; | ||
|
||
@Module({ | ||
providers: [AgentService], | ||
exports: [AgentService], | ||
}) | ||
export class AgentModule {} |
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,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Agent, Agents } from './agent.model'; | ||
import { AssistantCreateParams } from 'openai/resources/beta'; | ||
|
||
@Injectable() | ||
export class AgentService { | ||
public agents: Agents = {}; | ||
public tools: AssistantCreateParams.AssistantToolsFunction[] = []; | ||
|
||
add( | ||
definition: AssistantCreateParams.AssistantToolsFunction, | ||
fn: Agent, | ||
): void { | ||
this.tools.push(definition); | ||
this.agents[definition.function.name] = fn; | ||
} | ||
|
||
get(name: string): Agent { | ||
return this.agents[name]; | ||
} | ||
} |
File renamed without changes.
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
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
6 changes: 3 additions & 3 deletions
6
src/assistant/chatbot.service.ts → src/assistant/chatbot/chatbot.service.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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PokemonModule } from './pokemon/pokemon.module'; | ||
|
||
@Module({ | ||
imports: [PokemonModule], | ||
}) | ||
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,46 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AssistantCreateParams } from 'openai/resources/beta'; | ||
import { AgentData } from '../../../assistant/agent/agent.model'; | ||
import { GetPokemonParamsDto } from './get-pokemon.model'; | ||
import { AgentService } from '../../../assistant/agent/agent.service'; | ||
import { PokemonService } from './pokemon.service'; | ||
import { AgentBase } from '../../../assistant/agent/agent.base'; | ||
|
||
@Injectable() | ||
export class GetPokemonAgent extends AgentBase { | ||
definition: AssistantCreateParams.AssistantToolsFunction = { | ||
type: 'function', | ||
function: { | ||
name: 'getPokemon', | ||
description: 'Get pokemon stats and types', | ||
parameters: { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
description: 'The name of the pokemon provided by user', | ||
}, | ||
}, | ||
required: ['name'], | ||
}, | ||
}, | ||
}; | ||
|
||
constructor( | ||
protected readonly agentService: AgentService, | ||
private readonly pokemonService: PokemonService, | ||
) { | ||
super(agentService); | ||
} | ||
|
||
async output(data: AgentData): Promise<string> { | ||
try { | ||
const parsedData = JSON.parse(data?.params) as GetPokemonParamsDto; | ||
const pokemon = await this.pokemonService.getPokemon(parsedData?.name); | ||
|
||
return JSON.stringify(pokemon); | ||
} catch (errors) { | ||
return `Invalid data: ${JSON.stringify(errors)}`; | ||
} | ||
} | ||
} |
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,6 @@ | ||
import { IsNotEmpty } from 'class-validator'; | ||
|
||
export class GetPokemonParamsDto { | ||
@IsNotEmpty() | ||
name: string; | ||
} |
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,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { PokemonService } from './pokemon.service'; | ||
import { GetPokemonAgent } from './get-pokemon.agent'; | ||
import { AgentModule } from '../../../assistant/agent/agent.module'; | ||
|
||
@Module({ | ||
imports: [ConfigModule, HttpModule, AgentModule], | ||
providers: [PokemonService, GetPokemonAgent], | ||
}) | ||
export class PokemonModule {} |
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,22 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { firstValueFrom, map } from 'rxjs'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class PokemonService { | ||
apiUrl = this.configService.get('POKEMON_API_URL'); | ||
|
||
constructor( | ||
private readonly httpService: HttpService, | ||
private readonly configService: ConfigService, | ||
) {} | ||
|
||
async getPokemon(name: string): Promise<string> { | ||
return firstValueFrom( | ||
this.httpService | ||
.get(`${this.apiUrl}/pokemon/${name.toLowerCase()}`) | ||
.pipe(map(res => res.data)), | ||
); | ||
} | ||
} |
Oops, something went wrong.