Skip to content

Commit

Permalink
chore: cleanup types
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Nov 6, 2024
1 parent d459131 commit 892a2a2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 57 deletions.
5 changes: 4 additions & 1 deletion src/adapters/openai/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export class Completions {

constructor(context: Context) {
const {
env,
config: {
aiConfig: { baseUrl, kind },
},
env,
} = context;
const apiKey = kind === "OpenAi" ? env.OPENAI_API_KEY : env.OPENROUTER_API_KEY;

Expand Down Expand Up @@ -79,7 +79,9 @@ export class Completions {
}): Promise<string> {
const config = PluginContext.getInstance().config;
const ctxWindow = this.createSystemMessage(params);

logger.info("ctxWindow:\n\n", { ctxWindow });

const res: OpenAI.Chat.Completions.ChatCompletion = await this.client.chat.completions.create({
model: params.model,
messages: ctxWindow,
Expand All @@ -92,6 +94,7 @@ export class Completions {
type: "text",
},
});

const answer = res.choices[0].message;
if (answer?.content) {
return answer.content;
Expand Down
45 changes: 5 additions & 40 deletions src/adapters/supabase/embeddings.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,7 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { logger } from "../../utils/logger";
import { VoyageAIClient } from "voyageai";
const VECTOR_SIZE = 1024;

export interface CommentType {
id: string;
plaintext: string;
markdown?: string;
author_id: number;
created_at: string;
modified_at: string;
embedding: number[];
}

export interface CommentSimilaritySearchResult {
comment_id: string;
comment_plaintext: string;
comment_issue_id: string;
similarity: number;
text_similarity: number;
}

export interface IssueSimilaritySearchResult {
issue_id: string;
issue_plaintext: string;
similarity: number;
text_similarity: number;
}

export interface DatabaseIssue {
id: string;
markdown?: string;
plaintext?: string;
payload?: Record<string, unknown>;
author_id: number;
created_at: string;
modified_at: string;
embedding: number[];
}
import { CommentSimilaritySearchResult, DatabaseItem, IssueSimilaritySearchResult } from "../../types/ai";

export class Embeddings {
protected supabase: SupabaseClient;
Expand All @@ -51,16 +15,16 @@ export class Embeddings {
this.voyage = client;
}

async getIssue(issueNodeId: string): Promise<DatabaseIssue[] | null> {
const { data, error } = await this.supabase.from("issues").select("*").eq("id", issueNodeId).returns<DatabaseIssue[]>();
async getIssue(issueNodeId: string): Promise<DatabaseItem[] | null> {
const { data, error } = await this.supabase.from("issues").select("*").eq("id", issueNodeId).returns<DatabaseItem[]>();
if (error) {
logger.error("Error getting issue", { error });
return null;
}
return data;
}

async getComment(commentNodeId: string): Promise<CommentType[] | null> {
async getComment(commentNodeId: string): Promise<DatabaseItem[] | null> {
const { data, error } = await this.supabase.from("issue_comments").select("*").eq("id", commentNodeId);
if (error) {
logger.error("Error getting comment", { error });
Expand Down Expand Up @@ -104,6 +68,7 @@ export class Embeddings {
}

async createEmbedding(input: { text?: string; prompt?: string } = {}): Promise<number[]> {
const VECTOR_SIZE = 1024;
const { text = null, prompt = null } = input;
if (text === null) {
return new Array(VECTOR_SIZE).fill(0);
Expand Down
21 changes: 5 additions & 16 deletions src/bot/features/commands/shared/ask-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,16 @@ import { GrammyContext } from "../../../helpers/grammy-context";
import { logHandle } from "../../../helpers/logging";
import { logger } from "../../../../utils/logger";
import { PluginContext } from "../../../../types/plugin-context-single";
import { CommentSimilaritySearchResult, IssueSimilaritySearchResult } from "../../../../types/ai";

const composer = new Composer<GrammyContext>();

const feature = composer.chatType(["group", "private", "supergroup", "channel"]);
export interface CommentSimilaritySearchResult {
comment_id: string;
comment_plaintext: string;
comment_issue_id: string;
similarity: number;
text_similarity: number;
}

export interface IssueSimilaritySearchResult {
issue_id: string;
issue_plaintext: string;
similarity: number;
text_similarity: number;
}

feature.command("ubiquityos", logHandle("command-ubiquityos"), chatAction("typing"), async (ctx) => {
const {
adapters: { ai, embeddings },
} = ctx;

const directives = [
"Extract Relevant Information: Identify key pieces of information, even if they are incomplete, from the available corpus.",
"Apply Knowledge: Use the extracted information and relevant documentation to construct an informed response.",
Expand Down Expand Up @@ -59,9 +46,11 @@ feature.command("ubiquityos", logHandle("command-ubiquityos"), chatAction("typin
...(issues?.map((issue: IssueSimilaritySearchResult) => issue.issue_plaintext) || []),
];
});

logger.info("Similar Text:\n\n", { similarText });
const rerankedText = similarText.length > 0 ? await embeddings.reRankResults(similarText, question) : [];
logger.info("Reranked Text:\n\n", { rerankedText: rerankedText });
logger.info("Reranked Text:\n\n", { rerankedText });

return ctx.reply(
await ai.createCompletion({
directives,
Expand Down
25 changes: 25 additions & 0 deletions src/types/ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export type DatabaseItem = {
id: string;
markdown?: string;
plaintext: string;
author_id: number;
payload?: Record<string, unknown>;
created_at: string;
modified_at: string;
embedding: number[];
};

export type CommentSimilaritySearchResult = {
comment_id: string;
comment_plaintext: string;
comment_issue_id: string;
similarity: number;
text_similarity: number;
};

export type IssueSimilaritySearchResult = {
issue_id: string;
issue_plaintext: string;
similarity: number;
text_similarity: number;
};

0 comments on commit 892a2a2

Please sign in to comment.