Skip to content

Commit

Permalink
Add functionality to query the codebase using Pinecone based on the u…
Browse files Browse the repository at this point in the history
…ser query
  • Loading branch information
aliiyuu committed Aug 16, 2024
1 parent 9e2ce49 commit 2b4d998
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 289 deletions.
19 changes: 15 additions & 4 deletions frontend/src/components/Chatbot.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ h1 {
opacity: 0.5;
}

#send-button {
#send-button, #query-button {
font-family: "Poppins", sans-serif;
font-weight: bold;
font-size: large;
Expand All @@ -92,25 +92,36 @@ h1 {
border-radius: 10px;
cursor: pointer;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
margin-left: 10px;
}

#send-button:disabled {
#send-button:disabled, #query-button:disabled {
background-color: #808080;
cursor: not-allowed;
}

#send-button:disabled:hover {
#send-button:disabled:hover, #query-button:disabled:hover {
background-color: #808080; /* Hover color for disabled state */
}

#send-button:not(:disabled):hover {
#send-button:not(:disabled):hover, #query-button:not(:disabled):hover {
background-color: #62aced; /* Hover color for enabled state */
}

.flex {
display:flex;
}

#submit-buttons {
display: flex;
}

@media only screen and (max-width: 406px) {
#send-button, #query-button {
margin-bottom: 10px;
}
}

#codebase-url {
font-family: "Poppins", sans-serif;
border-style: none;
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/components/Chatbot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sendMessage, downloadCodebase, deleteCodebase } from "../interface";
import { sendMessage, downloadCodebase, deleteCodebase, sendCodebaseQuery } from "../interface";
import "./Chatbot.css";

function Chatbot() {
Expand All @@ -15,7 +15,10 @@ function Chatbot() {
<div id="messages"></div>
</div>
<textarea id="user-input" placeholder="Type your message here..."></textarea>
<button id="send-button" onClick={ sendMessage }>Enter</button>
<div className="submit-buttons">
<button id="send-button" onClick={ sendMessage }>Enter</button>
<button id="query-button" onClick={ sendCodebaseQuery }>Get relevant files</button>
</div>
</div>
</div>
);
Expand Down
45 changes: 44 additions & 1 deletion frontend/src/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export async function downloadCodebase() {
const local = 'http://localhost:3000';
const apiURL = 'https://syntaxsorcerer-backend.fly.dev'
const url = document.getElementById('codebase-url').value;
if (url.trim() === '') return;

const response = await fetch(`${local}/api/download`, {
method: 'POST',
Expand Down Expand Up @@ -53,11 +52,33 @@ export async function sendMessage() {
document.getElementById('user-input').value = '';

const sendButton = document.getElementById('send-button');
const queryButton = document.getElementById('query-button');
queryButton.disabled = true;
sendButton.disabled = true;

await fetchChatGPTResponse(userInput);

sendButton.disabled = false;
queryButton.disabled = false;
}

// Create a query from the user input that will be used to find the most relevant files
export async function sendCodebaseQuery() {
const userInput = document.getElementById('user-input').value;
if (userInput.trim() === '') return;

appendMessage('You', userInput);
document.getElementById('user-input').value = '';

const sendButton = document.getElementById('send-button');
const queryButton = document.getElementById('query-button');
queryButton.disabled = true;
sendButton.disabled = true;

await fetchPineconeResponse(userInput);

sendButton.disabled = false;
queryButton.disabled = false;
}

// Display a message on the frontend
Expand Down Expand Up @@ -103,3 +124,25 @@ export async function fetchChatGPTResponse(userInput) {
appendMessage('Assistant', botMessage.text);
}
}

// Fetch a response from the Pinecone API, generate an embedding from the user input
export async function fetchPineconeResponse(userInput) {
const ec2 = 'https://18.226.98.245:443';
const local = 'http://localhost:3000';
const apiURL = 'https://syntaxsorcerer-backend.fly.dev'

const response = await fetch(`${local}/api/pinecone`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ prompt: userInput })
});

const botMessage = await response.json();
if (botMessage.error) {
appendMessage('Error', botMessage.error);
} else {
appendMessage('Assistant', botMessage.text);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
const { OpenAI } = require('openai');
require('dotenv').config()
// Function to generate embeddings using OpenAI's API
async function generateEmbeddings(tokens) {
// Initialize OpenAI client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // Ensure your API key is set in the environment variables
});

try {
// Flatten the tokens into a format suitable for OpenAI
const text = tokens.map(token => JSON.stringify(token)).join('\n');

// Request embeddings
const response = await openai.embeddings.create({
model: 'text-embedding-ada-002', // Use an appropriate embedding model
input: text,
encoding_format: 'float'
});

console.log('Embedding Dimension: ', response.data[0].embedding.length);
console.log('OpenAI embeddings response:', response.data);
return response.data


} catch (error) {
console.error('Error generating embeddings with OpenAI:', error);
}
}

module.exports = generateEmbeddings;
const openai = require('../openaiConfig')
require('dotenv').config();

// Function to generate embeddings using OpenAI's API
async function generateEmbeddings(tokens) {
try {
// Flatten the tokens into a format suitable for OpenAI
const text = tokens.map(token => JSON.stringify(token)).join('\n');

// Request embeddings
const response = await openai.embeddings.create({
model: 'text-embedding-ada-002', // Use an appropriate embedding model
input: text,
encoding_format: 'float'
});

console.log('Embedding Dimension: ', response.data[0].embedding.length);
console.log('OpenAI embeddings response:', response.data);
return response.data;


} catch (error) {
console.error('Error generating embeddings with OpenAI:', error);
}
}

module.exports = generateEmbeddings;
5 changes: 5 additions & 0 deletions server/config/pineconeConfig/pineconeInit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const PineconeManager = require('../pineconeConfig/pineconeManager');

const pinecone = new PineconeManager(process.env.PINECONE_API_KEY, "syntaxsorcerer");

module.exports = pinecone;
Loading

0 comments on commit 2b4d998

Please sign in to comment.