-
Notifications
You must be signed in to change notification settings - Fork 2
feat(ai): added base for ai integration #589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2b848d2
init openai
slaveeks bfbd273
Merge branch 'master' of github.com:codex-team/hawk.api.nodejs into f…
slaveeks 349094c
feat(ai): added base for ai suggestions
slaveeks c6dfb77
Merge branch 'master' of github.com:codex-team/hawk.api.nodejs into f…
slaveeks 1b1d83d
fixes
slaveeks f70a64f
Bump version up to 1.2.23
github-actions[bot] 3c3f335
rm redundant packages
slaveeks 46f2b74
fix zod
slaveeks baffb2b
fixes
slaveeks a63f88a
review changes
slaveeks 018b70a
add todo
slaveeks ee14c20
added ai service, some refactorng
slaveeks 0c245b2
improve docs
slaveeks 57dae6e
improve prompts
slaveeks ac48e5c
improve prompts
slaveeks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,41 @@ | ||
| import { EventAddons, EventData } from '@hawk.so/types'; | ||
| import { generateText } from 'ai'; | ||
| import { openai } from '@ai-sdk/openai'; | ||
| import { eventSolvingInput } from './inputs/eventSolving'; | ||
| import { ctoInstruction } from './instructions/cto'; | ||
|
|
||
| /** | ||
| * Interface for interacting with Vercel AI Gateway | ||
| */ | ||
| class VercelAIApi { | ||
| /** | ||
| * Model ID to use for generating suggestions | ||
| */ | ||
| private readonly modelId: string; | ||
|
|
||
| constructor() { | ||
| /** | ||
| * @todo make it dynamic, get from project settings | ||
| */ | ||
| this.modelId = 'gpt-4o'; | ||
| } | ||
|
|
||
| /** | ||
| * Generate AI suggestion for the event | ||
| * | ||
| * @param {EventData<EventAddons>} payload - event data | ||
| * @returns {Promise<string>} AI suggestion for the event | ||
| * @todo add defence against invalid prompt injection | ||
| */ | ||
| public async generateSuggestion(payload: EventData<EventAddons>) { | ||
| const { text } = await generateText({ | ||
| model: openai(this.modelId), | ||
| system: ctoInstruction, | ||
| prompt: eventSolvingInput(payload), | ||
| }); | ||
|
|
||
| return text; | ||
| } | ||
| } | ||
|
|
||
| export const vercelAIApi = new VercelAIApi(); | ||
This file contains hidden or 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,5 @@ | ||
| import { EventData, EventAddons } from '@hawk.so/types'; | ||
|
|
||
| export const eventSolvingInput = (payload: EventData<EventAddons>) => ` | ||
| Payload: ${JSON.stringify(payload)} | ||
| `; |
This file contains hidden or 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,9 @@ | ||
| export const ctoInstruction = `Ты технический директор ИТ компании, тебе нужно пояснить ошибку и предложить решение. | ||
|
|
||
| Предоставь ответ в следующем формате: | ||
|
|
||
| 1. Описание проблемы | ||
| 2. Решение проблемы | ||
| 3. Описание того, как можно предотвратить подобную ошибку в будущем | ||
|
|
||
| Ответь на русском языке.`; |
This file contains hidden or 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 hidden or 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 hidden or 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 { vercelAIApi } from '../integrations/vercel-ai/'; | ||
| import { EventsFactoryInterface } from './types'; | ||
|
|
||
| /** | ||
| * Service for interacting with AI | ||
| */ | ||
| export class AIService { | ||
| /** | ||
| * Generate suggestion for the event | ||
| * | ||
| * @param eventsFactory - events factory | ||
| * @param eventId - event id | ||
| * @param originalEventId - original event id | ||
| * @returns {Promise<string>} - suggestion | ||
| */ | ||
| public async generateSuggestion(eventsFactory: EventsFactoryInterface, eventId: string, originalEventId: string): Promise<string> { | ||
| const event = await eventsFactory.getEventRepetition(eventId, originalEventId); | ||
|
|
||
| if (!event) { | ||
| throw new Error('Event not found'); | ||
| } | ||
|
|
||
| return vercelAIApi.generateSuggestion(event.payload); | ||
| } | ||
| } | ||
|
|
||
| export const aiService = new AIService(); |
This file contains hidden or 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,23 @@ | ||
| import { EventAddons, EventData } from '@hawk.so/types'; | ||
|
|
||
| /** | ||
| * Event type which is returned by events factory | ||
| */ | ||
| type Event = { | ||
| _id: string; | ||
| payload: EventData<EventAddons>; | ||
| }; | ||
|
|
||
| /** | ||
| * Interface for interacting with events factory | ||
| */ | ||
| export interface EventsFactoryInterface { | ||
| /** | ||
| * Get event repetition | ||
| * | ||
| * @param repetitionId - repetition id | ||
| * @param originalEventId - original event id | ||
| * @returns {Promise<EventData<EventAddons>>} - event repetition | ||
| */ | ||
| getEventRepetition(repetitionId: string, originalEventId: string): Promise<Event>; | ||
| } |
This file contains hidden or 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 hidden or 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,18 @@ | ||
| declare module 'ai' { | ||
| /** | ||
| * Minimal type for generateText used in server-side integration. | ||
| */ | ||
| export function generateText(input: { | ||
| model: any; | ||
| system?: string; | ||
| prompt: string; | ||
| }): Promise<{ text: string }>; | ||
| } | ||
|
|
||
| declare module '@ai-sdk/openai' { | ||
| /** | ||
| * Minimal types for OpenAI provider. | ||
| */ | ||
| export function createOpenAI(config?: { apiKey?: string }): (model: string) => any; | ||
| export const openai: (model: string) => any; | ||
| } |
This file contains hidden or 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,5 @@ | ||
| declare module 'zod/v4' { | ||
| export * from 'zod'; | ||
| import z from 'zod'; | ||
| export default z; | ||
| } |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.