Skip to content

Commit

Permalink
refactoring the code
Browse files Browse the repository at this point in the history
  • Loading branch information
md-abid-hussain committed Jan 27, 2025
1 parent 6ebabcb commit c10e141
Show file tree
Hide file tree
Showing 24 changed files with 81 additions and 129 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,34 @@
DocQuery is a comprehensive project consisting of both frontend and backend components. The frontend is built with Next.js and Tailwind CSS, providing a user-friendly interface to create powerful knowledge bases for LLMs using markdown documentation. The backend leverages FastAPI to provide endpoints for different agents built using the `langgraph` library and integrated into the FastAPI application using `CopilotKitSDK`.

## Video: Click to view

[![Youtube Video](https://img.youtube.com/vi/nYDthsB8d7I/maxresdefault.jpg)](https://youtu.be/nYDthsB8d7I?si=uOEHGZB2A7DRpMsn)

## Setup

### Frontend

1. Clone the repository:

```sh
git clone <repository-url>
cd docquery-frontend
```

2. Install dependencies:

```sh
npm install
```

3. Copy the example environment file and fill in the required values:

```sh
cp .env.example .env
```

4. Start the development server:

```sh
npm run dev
```
Expand All @@ -37,28 +42,33 @@ DocQuery is a comprehensive project consisting of both frontend and backend comp
### Backend

1. Clone the repository:

```sh
git clone <repository-url>
cd docquery-backend
```

2. Create and activate a virtual environment:

```sh
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
```

3. Install dependencies:

```sh
pip install -r requirements.txt
```

4. Copy the example environment file and fill in the required values:

```sh
cp .env.example .env
```

5. Start the FastAPI server:

```sh
uvicorn app.main:app --reload
```
Expand All @@ -79,12 +89,14 @@ DocQuery is a comprehensive project consisting of both frontend and backend comp
#### Ingestion Agent

The Ingestion Agent is defined in agent.py. It consists of the following nodes:

- `Ingestion Node`
- `Verify Ingestion Node`

#### QA Agent

The QA Agent is defined in agent.py. It consists of the following nodes:

- `Retrieve Node`
- `Chat Node`

Expand Down
19 changes: 7 additions & 12 deletions docquery-backend/app/agents/qa_agent/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

model = get_chat_model()


def get_similar_documents(repo_name: str, query: str) -> List[Document]:
retriever = MongoDBAtlasHybridSearchRetriever(
vectorstore=get_vector_store(),
Expand All @@ -22,15 +23,6 @@ def get_similar_documents(repo_name: str, query: str) -> List[Document]:


async def retrieve_node(state: QAAgentState, config: RunnableConfig):
if state.get("prev_repo_name"):
print("got previous state")
if state["prev_repo_name"] != state["repository_name"]:
state["documents"] = None
state['messages'] = []
state["prev_repo_name"] = state["repository_name"]

else:
state["prev_repo_name"] = state["repository_name"]

question = state["question"]
repository_name = state["repository_name"]
Expand All @@ -44,12 +36,15 @@ async def retrieve_node(state: QAAgentState, config: RunnableConfig):
async def chat_node(state: QAAgentState, config: RunnableConfig):

system_message = f"""
Answer the user\'s question from the context. If context not present try answering yourself.
Answer the user\'s question from the context.
If context not present try answering yourself.
user_question: {state["question"]}
context: {"\n\n".join(doc.page_content for doc in state["documents"])}
Answer should be detailed and should contain examples for explanation if required.
If context is empty return a message saying that not able to find any answer.
Answer should be detailed and should contain examples for
explanation if required.
If context is empty return a message saying that not able
to find any answer.
"""

messages = [
Expand Down
1 change: 0 additions & 1 deletion docquery-backend/app/agents/qa_agent/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
class QAAgentState(CopilotKitState):
question: str
repository_name: str
prev_repo_name: Optional[str]
documents: Optional[List[Document]]
error: Optional[str]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { useParams } from 'next/navigation';
import { useEffect } from 'react';
import { useCoAgent, useCopilotReadable, useCopilotChat } from '@copilotkit/react-core';
import { useCoAgent, useCopilotReadable } from '@copilotkit/react-core';
import { CopilotChat } from '@copilotkit/react-ui';
import { QAAgentState } from '@/lib/types';
import useRepoDetails from '@/hooks/useRepoDetails';
Expand All @@ -15,20 +15,20 @@ const ChatRepoPage = () => {
return <div>Loading...</div>;
}

const { repoDetails, loading, error } = useRepoDetails(owner as string, repo as string);
const { repoDetails } = useRepoDetails(owner as string, repo as string);

useCopilotReadable({
description: "use qa agent to answer this",
value: "qa_agent",
});


const { state, setState } = useCoAgent<QAAgentState>({
name: "qa_agent",
initialState: {
question: "",
repository_name: `${owner}/${repo}`,
messages:[]
messages: []
}
});

Expand All @@ -49,7 +49,10 @@ const ChatRepoPage = () => {
</div>
<div className="flex flex-col">
<CopilotChat
labels={{ title: `Ask any question related to $` }}
labels={{
title: "Your Assistant",
initial: "Hi! 👋 How can I assist you today?",
}}
onSubmitMessage={(message) => {
setState({ ...state, question: message });
}}
Expand Down
Binary file modified docquery-frontend/src/app/favicon.ico
Binary file not shown.
2 changes: 1 addition & 1 deletion docquery-frontend/src/app/markdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,5 @@
}

.copilotKitCodeBlockToolbarButton:hover {
background-color: rgb(55, 55, 58);
background-color: rgb(0, 0, 0);
}
10 changes: 5 additions & 5 deletions docquery-frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Link from "next/link"
import { Hero } from "@/components/other/hero"
import { CTA } from "@/components/other/cta"
import { Features } from "@/components/other/features"
import { Benefits } from "@/components/other/benefits"
import { TechStack } from "@/components/other/tech-stack"
import { Hero } from "@/components/landing-page-components/hero"
import { CTA } from "@/components/landing-page-components/cta"
import { Features } from "@/components/landing-page-components/features"
import { Benefits } from "@/components/landing-page-components/benefits"
import { TechStack } from "@/components/landing-page-components/tech-stack"

import { Mountain } from "lucide-react"

Expand Down
7 changes: 0 additions & 7 deletions docquery-frontend/src/components/LoadFiles.tsx

This file was deleted.

8 changes: 4 additions & 4 deletions docquery-frontend/src/components/copilot/code-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export { CodeBlock };
// (Next.js classic) setup.
const highlightStyle: any = {
'pre[class*="language-"]': {
color: "#d4d4d4",
color: "#ffffff",
fontSize: "13px",
textShadow: "none",
fontFamily: 'Menlo, Monaco, Consolas, "Andale Mono", "Ubuntu Mono", "Courier New", monospace',
Expand All @@ -98,10 +98,10 @@ const highlightStyle: any = {
padding: "1em",
margin: ".5em 0",
overflow: "auto",
background: "#1e1e1e",
background: "#000000",
},
'code[class*="language-"]': {
color: "#d4d4d4",
color: "#ffffff",
fontSize: "13px",
textShadow: "none",
fontFamily: 'Menlo, Monaco, Consolas, "Andale Mono", "Ubuntu Mono", "Courier New", monospace',
Expand Down Expand Up @@ -139,7 +139,7 @@ const highlightStyle: any = {
padding: ".1em .3em",
borderRadius: ".3em",
color: "#db4c69",
background: "#1e1e1e",
background: "#000000",
},
".namespace": {
Opacity: ".7",
Expand Down
71 changes: 36 additions & 35 deletions docquery-frontend/src/components/copilot/copilot-text-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,48 +38,49 @@ const CopilotTextMessage = ({ renderMessageProps, repoProps }: CopilotTextMessag
const { message, isCurrentMessage, inProgress } = renderMessageProps;
const { repoName, imageUrl } = repoProps;

if (message.isTextMessage()) {
if (message.role == "user") {
return (
<div className="group relative flex items-center gap-3 px-4 py-2 animate-in fade-in slide-in-from-bottom-2 duration-300 ease-out opacity-70 flex-row-reverse"
>
<MessageAvatar role="user" />
<div className={"flex flex-col items-end max-w-[70%]"}>
<span className="text-sm font-medium text-muted-foreground mb-1">
{"You"}
</span>
<div className="rounded-2xl px-4 py-3 w-full bg-primary text-primary-foreground"
>
{message.content}
</div>
if (!message.isTextMessage()) {
return null;
}

if (message.role === "user") {
return (
<div className="group relative flex items-center gap-3 px-4 py-2 animate-in fade-in slide-in-from-bottom-2 duration-300 ease-out opacity-70 flex-row-reverse">
<MessageAvatar role="user" />
<div className="flex flex-col items-end max-w-[70%]">
<span className="text-sm font-medium text-muted-foreground mb-1">
{"You"}
</span>
<div className="rounded-2xl px-4 py-3 w-full bg-primary text-primary-foreground">
{message.content}
</div>
</div>
)
}
</div>
);
}

else if (message.role == "assistant") {
return (
<div>
{isCurrentMessage && inProgress && !message.content ? <p>Loading</p> : (
<div className="group relative flex items-center gap-3 px-4 py-2 animate-in fade-in slide-in-from-bottom-2 duration-300 ease-out opacity-70 flex-row"
>
<MessageAvatar role={repoName} url={imageUrl} />
<div className={"flex flex-col items-start max-w-[70%]"}>
<span className="text-sm font-medium text-muted-foreground mb-1">
{repoName}
</span>
<div className="rounded-2xl px-4 py-3 w-full bg-secondary text-secondary-foreground"
>
<Markdown content={message.content} />
</div>
if (message.role === "assistant") {
return (
<div>
{isCurrentMessage && inProgress && !message.content ? (
<p>Loading</p>
) : (
<div className="group relative flex items-end gap-3 px-4 py-2 animate-in fade-in slide-in-from-bottom-2 duration-300 ease-out opacity-70 flex-row">
<MessageAvatar role="assistant" url={imageUrl} />
<div className="flex flex-col items-start max-w-[70%]">
<span className="text-sm font-medium text-muted-foreground mb-1">
{repoName}
</span>
<div className="rounded-2xl px-4 py-3 w-full bg-secondary">
<Markdown content={message.content} />
</div>
</div>
)}
</div>)
}
</div>
)}
</div>
);
}


return null;
};

export default CopilotTextMessage;
2 changes: 1 addition & 1 deletion docquery-frontend/src/components/copilot/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const Markdown = ({ content }: MarkdownProps) => {

const components: Components = {
p({ children }) {
return <p>{children}</p>;
return <p className="text-black">{children}</p>;
},
a({ children, ...props }) {
return (
Expand Down
4 changes: 1 addition & 3 deletions docquery-frontend/src/components/github-repo-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const GitHubRepoCard: React.FC<GitHubRepoCardProps> = ({ repoName }) => {
const [error, setError] = useState<string | null>(null);
const [owner, repo] = repoName.split('/');


const formatStarCount = (count: number) => {
if (count >= 1000) {
return `${(count / 1000).toFixed(1)}k`;
Expand All @@ -30,7 +29,6 @@ const GitHubRepoCard: React.FC<GitHubRepoCardProps> = ({ repoName }) => {
setError(null);

try {

const response = await fetch(`https://api.github.com/repos/${owner}/${repo}`);
if (!response.ok) {
throw new Error(response.status === 404 ? 'Repository not found' : 'Failed to fetch repository info');
Expand All @@ -56,7 +54,7 @@ const GitHubRepoCard: React.FC<GitHubRepoCardProps> = ({ repoName }) => {
};

fetchRepoInfo();
}, [repoName]);
}, []);

return (
<div >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React, { useState } from 'react'
import { Checkbox } from "@/components/ui/checkbox"
import { Button } from "@/components/ui/button"
import { ChevronDown, ChevronRight, File, Folder, ChevronUp } from 'lucide-react'
import { ChevronDown, ChevronRight, File, ChevronUp } from 'lucide-react'
import { parseFileTree, FileNode, getAllDescendantPaths } from '@/lib/fileTree'
import { cn } from '@/lib/utils'
import { Card } from './ui/card'
Expand Down
4 changes: 2 additions & 2 deletions docquery-frontend/src/components/repo-details.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { GithubCard } from '@/components/GithubCard';
import { GitHubRepoFiles } from '@/components/GithubRepoFiles';
import { GithubCard } from '@/components/github-card';
import { GitHubRepoFiles } from '@/components/github-repo-files';
import { RepoInfo } from '@/lib/types';

interface RepoDetailsProps {
Expand Down
11 changes: 0 additions & 11 deletions docquery-frontend/src/components/theme-provider.tsx

This file was deleted.

Loading

0 comments on commit c10e141

Please sign in to comment.