-
Notifications
You must be signed in to change notification settings - Fork 49
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
✨ Add knowledge retrieval by using RAG #805
Open
sasamuku
wants to merge
10
commits into
main
Choose a base branch
from
add_rag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6f1fd31
🔧 Fix Supabase CLI pull command in package.json
sasamuku 0dfd612
🔧 Add documents table with vector embeddings and matching function
sasamuku b85c609
✨ Add URL content vectorization feature to migration web app
sasamuku a3f8880
🔧 Exclude migration-web from workspace packages
sasamuku ba3665c
🔧 Approve @browserbasehq/sdk package license
sasamuku ba2c5bc
fixup! ✨ Add URL content vectorization feature to migration web app
sasamuku d7d4e68
✨ Add knowledge retrieval feature for database schema advice
sasamuku c377832
✨ Add text content vectorization feature to migration web app
sasamuku 6ad5953
🔧 Refactor retrieval template for more general AI assistance
sasamuku 81ac3b5
✨ Add responsive header with navigation for migration web app
sasamuku 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 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,94 @@ | ||
import { langfuseLangchainHandler, vectorStore } from '@/lib' | ||
import { PromptTemplate } from '@langchain/core/prompts' | ||
import { ChatOpenAI } from '@langchain/openai' | ||
import { HttpResponseOutputParser } from 'langchain/output_parsers' | ||
import type { NextRequest } from 'next/server' | ||
|
||
export const runtime = 'edge' | ||
|
||
const RETRIEVAL_TEMPLATE = `You are a knowledgeable assistant that helps users with their questions by providing accurate and helpful information. | ||
|
||
I'll provide you with: | ||
1. The user's query | ||
2. Relevant context retrieved from our knowledge base | ||
|
||
Based on this information, please provide a detailed response that: | ||
- Directly addresses the user's question | ||
- Incorporates relevant information from the provided context | ||
- Provides clear explanations and examples where appropriate | ||
- Highlights important considerations or best practices | ||
- Cites specific information from the context when relevant | ||
|
||
User Query: | ||
""" | ||
{query} | ||
""" | ||
|
||
Relevant Context: | ||
""" | ||
{context} | ||
""" | ||
|
||
Instructions for your response: | ||
1. If the context contains relevant information, use it to enhance your answer | ||
2. If the context doesn't contain enough information, acknowledge this and provide the best general guidance you can | ||
3. If you're unsure about something, be transparent about the limitations of your knowledge | ||
4. Format your response in Markdown to improve readability | ||
5. Keep your response practical, specific, and actionable | ||
6. Do not mention that you are using context or reference the retrieval process in your answer | ||
|
||
Please provide a comprehensive and helpful response.` | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
const { query } = await req.json() | ||
|
||
if (!query || typeof query !== 'string') { | ||
return new Response( | ||
JSON.stringify({ | ||
error: 'Query is not provided or is in an invalid format', | ||
}), | ||
{ status: 400 }, | ||
) | ||
} | ||
|
||
const retriever = vectorStore.asRetriever({ | ||
searchType: 'similarity', | ||
k: 5, | ||
}) | ||
|
||
const relevantDocs = await retriever.invoke(query) | ||
const context = relevantDocs.map((doc) => doc.pageContent).join('\n\n') | ||
|
||
const prompt = PromptTemplate.fromTemplate(RETRIEVAL_TEMPLATE) | ||
|
||
const model = new ChatOpenAI({ | ||
temperature: 0.7, | ||
model: 'gpt-4o-mini', | ||
}) | ||
|
||
const outputParser = new HttpResponseOutputParser() | ||
|
||
const chain = prompt.pipe(model).pipe(outputParser) | ||
|
||
const stream = await chain.stream( | ||
{ | ||
query: query, | ||
context: context, | ||
}, | ||
{ | ||
callbacks: [langfuseLangchainHandler], | ||
}, | ||
) | ||
|
||
return new Response(stream) | ||
} catch (error) { | ||
console.error('Error in retrieve API:', error) | ||
return new Response( | ||
JSON.stringify({ | ||
error: 'An error occurred while retrieving knowledge', | ||
}), | ||
{ status: 500 }, | ||
) | ||
} | ||
} |
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,49 @@ | ||
import { vectorizeText, vectorizeUrl } from '@/lib/vectorization' | ||
import type { NextRequest } from 'next/server' | ||
|
||
export const runtime = 'edge' | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
const { url, text } = await req.json() | ||
|
||
if (url && typeof url === 'string') { | ||
const result = await vectorizeUrl(url) | ||
return new Response( | ||
JSON.stringify({ | ||
success: true, | ||
message: 'URL content vectorized and stored successfully', | ||
id: result.documentId, | ||
chunkCount: result.chunkCount, | ||
}), | ||
{ status: 200 }, | ||
) | ||
} | ||
if (text && typeof text === 'string') { | ||
const result = await vectorizeText(text) | ||
return new Response( | ||
JSON.stringify({ | ||
success: true, | ||
message: 'Text content vectorized and stored successfully', | ||
id: result.documentId, | ||
chunkCount: result.chunkCount, | ||
}), | ||
{ status: 200 }, | ||
) | ||
} | ||
return new Response( | ||
JSON.stringify({ | ||
error: 'Neither URL nor text is provided or is in an invalid format', | ||
}), | ||
{ status: 400 }, | ||
) | ||
} catch (error) { | ||
console.error('Error in vectorize API:', error) | ||
return new Response( | ||
JSON.stringify({ | ||
error: 'An error occurred while processing the request', | ||
}), | ||
{ status: 500 }, | ||
) | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
frontend/apps/migration-web/app/knowledge_retrieval/page.module.css
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,15 @@ | ||
.container { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 2rem; | ||
padding: 2rem; | ||
max-width: 1200px; | ||
margin: 0 auto; | ||
} | ||
|
||
.title { | ||
font-size: 2rem; | ||
font-weight: 600; | ||
color: #333; | ||
margin-bottom: 1rem; | ||
} |
14 changes: 14 additions & 0 deletions
14
frontend/apps/migration-web/app/knowledge_retrieval/page.tsx
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,14 @@ | ||
import { KnowledgeRetrievalWindow } from '@/components/KnowledgeRetrievalWindow' | ||
import styles from './page.module.css' | ||
|
||
export default function KnowledgeRetrievalPage() { | ||
return ( | ||
<div className={styles.container}> | ||
<h1 className={styles.title}>Database Knowledge Retrieval</h1> | ||
<KnowledgeRetrievalWindow | ||
endpoint="api/retrieve" | ||
placeholder="Describe the database schema change you're planning to make..." | ||
/> | ||
</div> | ||
) | ||
} |
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,14 @@ | ||
.container { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 2rem; | ||
padding: 2rem; | ||
max-width: 1200px; | ||
margin: 0 auto; | ||
} | ||
|
||
.title { | ||
font-size: 2rem; | ||
font-weight: 600; | ||
margin-bottom: 1rem; | ||
} |
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,13 @@ | ||
import { TextVectorizer } from '@/components/TextVectorizer' | ||
import { UrlVectorizer } from '@/components/UrlVectorizer' | ||
import styles from './page.module.css' | ||
|
||
export default function VectorizePage() { | ||
return ( | ||
<div className={styles.container}> | ||
<h1 className={styles.title}>Vectorize Content</h1> | ||
<UrlVectorizer endpoint="api/vectorize" /> | ||
<TextVectorizer endpoint="api/vectorize" /> | ||
</div> | ||
) | ||
} |
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,66 @@ | ||
.header { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding: 1rem 2rem; | ||
background-color: #f8f9fa; | ||
border-bottom: 1px solid #e9ecef; | ||
width: 100%; | ||
} | ||
|
||
.logo { | ||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
.logo h1 { | ||
font-size: 1.5rem; | ||
font-weight: 600; | ||
color: #212529; | ||
margin: 0; | ||
} | ||
|
||
.nav { | ||
display: flex; | ||
gap: 1.5rem; | ||
} | ||
|
||
.navLink { | ||
color: #495057; | ||
text-decoration: none; | ||
font-weight: 500; | ||
padding: 0.5rem 0.75rem; | ||
border-radius: 4px; | ||
transition: all 0.2s ease; | ||
} | ||
|
||
.navLink:hover { | ||
color: #212529; | ||
background-color: #e9ecef; | ||
} | ||
|
||
.active { | ||
color: #0d6efd; | ||
background-color: rgba(13, 110, 253, 0.1); | ||
} | ||
|
||
.active:hover { | ||
color: #0d6efd; | ||
background-color: rgba(13, 110, 253, 0.15); | ||
} | ||
|
||
@media (max-width: 768px) { | ||
.header { | ||
flex-direction: column; | ||
padding: 1rem; | ||
} | ||
|
||
.logo { | ||
margin-bottom: 1rem; | ||
} | ||
|
||
.nav { | ||
width: 100%; | ||
justify-content: center; | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see 👀