Skip to content

Commit

Permalink
making progress
Browse files Browse the repository at this point in the history
  • Loading branch information
rschwabco committed Apr 1, 2024
1 parent 322465d commit 0c799fe
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const config = new Configuration({
const openai = new OpenAIApi(config)

// IMPORTANT! Set the runtime to edge
export const runtime = 'edge'
export const runtime = 'nodejs'

export async function POST(req: Request) {
try {
Expand All @@ -30,6 +30,7 @@ export async function POST(req: Request) {

// Join all the chunks of text together, truncate to the maximum number of tokens, and return the result
const contextText = docs.join("\n").substring(0, 3000)
console.log("contextText", contextText)

const prompt = [
{
Expand Down Expand Up @@ -58,7 +59,7 @@ export async function POST(req: Request) {

// Ask OpenAI for a streaming chat completion given the prompt
const response = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
model: 'gpt-4',
stream: true,
messages: [...prompt, ...sanitizedMessages.filter((message: Message) => message.role === 'user')]
})
Expand Down
17 changes: 14 additions & 3 deletions src/app/api/seed/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,25 @@ async function seed(indexName: string, options: SeedOptions) {
const vectors = await Promise.all(documents.flat().map(embedDocument));

// Create relations in the directory between the user and the documents
const user = {
const rick = {
id: 'rick@the-citadel.com',
picture: "https://www.topaz.sh/assets/templates/citadel/img/Rick%20Sanchez.jpg",
email: 'rick@the-citadel.com',
name: 'Rick Sanchez',
role: 'admin'
roles: ['admin']
}
const morty = {
"id": "morty@the-citadel.com",
"name": "Morty Smith",
"email": "morty@the-citadel.com",
"picture": "https://www.topaz.sh/assets/templates/citadel/img/Morty%20Smith.jpg",
"roles": [
"editor"
],
}
const user = morty

const relations = await assignRelation(user, vectors.slice(0, 2), 'owns');
const relations = await assignRelation(user, vectors.slice(3, 5), 'owner');
console.log(relations);

// Upsert vectors into the Pinecone index
Expand Down
16 changes: 14 additions & 2 deletions src/app/services/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PineconeRecord } from "@pinecone-database/pinecone";
import { getEmbeddings } from './embeddings';
import { getMatchesFromEmbeddings } from "./pinecone";
import { Permission, getFilteredMatches } from "./directory";

export type Metadata = {
url: string,
Expand All @@ -16,9 +17,20 @@ export const getContext = async (message: string, namespace: string, maxTokens =

// Retrieve the matches for the embeddings from the specified namespace
const matches = await getMatchesFromEmbeddings(embedding, 10, namespace);

const rick = {
id: 'rick@the-citadel.com',
picture: "https://www.topaz.sh/assets/templates/citadel/img/Rick%20Sanchez.jpg",
email: 'rick@the-citadel.com',
name: 'Rick Sanchez',
roles: ['admin']
}

const user = rick;
const filteredMatches = await getFilteredMatches(user, matches, Permission.READ);

console.log(filteredMatches);
// Filter out the matches that have a score lower than the minimum score
const qualifyingDocs = matches.filter(m => m.score && m.score > minScore);
const qualifyingDocs = filteredMatches.filter(m => m.score && m.score > minScore);

return qualifyingDocs

Expand Down
28 changes: 25 additions & 3 deletions src/app/services/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ImportOpCode,
objectPropertiesAsStruct
} from "@aserto/aserto-node";
import type { PineconeRecord } from "@pinecone-database/pinecone";
import type { PineconeRecord, ScoredPineconeRecord } from "@pinecone-database/pinecone";

const directoryClient = DirectoryServiceV3({
url: process.env.ASERTO_DIRECTORY_SERVICE_URL,
Expand Down Expand Up @@ -46,7 +46,13 @@ export interface User {
id: string;
email: string;
name: string;
role: string;
roles: string[];
}

export enum Permission {
READ = "read",
WRITE = "write",
DELETE = "delete",
}

export const assignRelation = async (user: User, documents: PineconeRecord[], relationName: string) => {
Expand All @@ -58,7 +64,7 @@ export const assignRelation = async (user: User, documents: PineconeRecord[], re
properties: objectPropertiesAsStruct({
email: user.email,
name: user.name,
role: user.role,
roles: user.roles,
}),
displayName: user.name,
};
Expand Down Expand Up @@ -118,4 +124,20 @@ export const assignRelation = async (user: User, documents: PineconeRecord[], re
console.error("Error importing request: ", error);
throw error;
}
}


export const getFilteredMatches = async (user: User, matches: ScoredPineconeRecord[], permission: Permission) => {
const checks = await Promise.all(matches.map(async (match) => {
const response = await directoryClient.checkPermission({
subjectId: user.id,
subjectType: "user",
objectId: match.id,
objectType: "resource",
permission: permission,
});
console.log("response", response)
return response ? response.check : false
}));
return matches.filter((match, index) => checks[index]);
}

0 comments on commit 0c799fe

Please sign in to comment.