-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assistant): support for assistant files and knowledge retrieval (#5
) * feat(assistant): created assistant module with empty services * feat(app): env config and removed unused default NestJS code * feat(assistant): created basic functionality for assistant service * feat(assistant): saved the asssitant ID in the env variables * feat(assistant): update the assistant when instance exist * feat(assistant): implemented full flow without additional agents * feat(assistant): adding files to the assistant * feat(assistant): support for assistant files and knowledge retrieval * feat(agent): added example of agent * feat: vercel configuration & github actions
- Loading branch information
1 parent
ba6813c
commit 7e2975e
Showing
29 changed files
with
437 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Vercel Production Deployment | ||
env: | ||
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} | ||
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} | ||
on: | ||
push: | ||
branches: | ||
- main | ||
- feat/assistant | ||
jobs: | ||
Deploy-Production: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Install Vercel CLI | ||
run: npm install --global vercel@latest | ||
- name: Pull Vercel Environment Information | ||
run: vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }} | ||
- name: Build Project Artifacts | ||
run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }} | ||
- name: Deploy Project Artifacts to Vercel | ||
run: vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { FileObject } from 'openai/resources'; | ||
import { createReadStream } from 'fs'; | ||
import { AiService } from './ai/ai.service'; | ||
import { AssistantConfig } from './assistant.model'; | ||
|
||
@Injectable() | ||
export class AssistantFilesService { | ||
constructor( | ||
@Inject('config') private config: AssistantConfig, | ||
private readonly aiService: AiService, | ||
) {} | ||
|
||
async create( | ||
fileNames: string[], | ||
fileDir = this.config.filesDir, | ||
): Promise<string[]> { | ||
const files: FileObject[] = []; | ||
|
||
for (const name of fileNames) { | ||
const file = await this.aiService.provider.files.create({ | ||
file: createReadStream(`${fileDir || ''}/${name}`), | ||
purpose: 'assistants', | ||
}); | ||
|
||
files.push(file); | ||
} | ||
|
||
return files.map(({ id }) => id); | ||
} | ||
} |
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,27 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { writeFile, readFile } from 'fs/promises'; | ||
import * as envfile from 'envfile'; | ||
import * as process from 'process'; | ||
|
||
@Injectable() | ||
export class AssistantMemoryService { | ||
private readonly logger = new Logger(AssistantMemoryService.name); | ||
|
||
async saveAssistantId(id: string): Promise<void> { | ||
try { | ||
const sourcePath = './.env'; | ||
const envVariables = await readFile(sourcePath); | ||
const parsedVariables = envfile.parse(envVariables.toString()); | ||
const newVariables = { | ||
...parsedVariables, | ||
ASSISTANT_ID: id, | ||
}; | ||
|
||
process.env.ASSISTANT_ID = id; | ||
|
||
await writeFile(sourcePath, envfile.stringify(newVariables)); | ||
} catch (error) { | ||
this.logger.error(`Can't save variable: ${error}`); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.