Skip to content

Commit

Permalink
for document
Browse files Browse the repository at this point in the history
  • Loading branch information
taichan03 committed Aug 24, 2024
1 parent 89d5211 commit 86fb52b
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config/env/env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ LOGIN_REDIRECT_URL=
OPENAI_API_KEY=
PINECONE_API_KEY=
[email protected]
EMAIL_HOST_PASSWORD=lrclncfsoeyldbzo
EMAIL_HOST_PASSWORD=
4 changes: 2 additions & 2 deletions frontend/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VITE_API_BASE_URL=https://balancertestsite.com/
# VITE_API_BASE_URL=http://localhost:8000
# VITE_API_BASE_URL=https://balancertestsite.com/
VITE_API_BASE_URL=http://localhost:8000
15 changes: 8 additions & 7 deletions frontend/src/pages/DrugSummary/DrugSummaryForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const DrugSummaryForm = () => {

const sendMessage = (message: ChatLogItem[]) => {
const baseUrl = import.meta.env.VITE_API_BASE_URL;
const url = `${baseUrl}/chatgpt/chat`;
const url = `${baseUrl}/v1/api/embeddings/ask_embeddings`;

const apiMessages = message.map((messageObject) => {
let role = "";
Expand All @@ -128,15 +128,14 @@ const DrugSummaryForm = () => {
return { role: role, content: messageObject.message };
});

setIsLoading(true);
systemMessage.content += `If applicable, please use the following content to ask questions. If not applicable,
please answer to the best of your ability: ${pageContent}`;

const apiRequestBody = {
prompt: [systemMessage, ...apiMessages],
message: [apiMessages],
};

setIsLoading(true);

axios
.post(url, apiRequestBody)
.then((response) => {
Expand Down Expand Up @@ -219,9 +218,11 @@ const DrugSummaryForm = () => {
</div>
))
)}
{isLoading && (
{!isLoading && (
<div key={chatLog.length} className="flex justify-between">
<div className="max-w-sm rounded-lg p-4 text-white"></div>
<div className="items-center justify-center p-1">
<span className="thinking">Let's me think</span>
</div>
</div>
)}
<div className="h-[100px]"> </div>
Expand Down Expand Up @@ -265,7 +266,7 @@ const DrugSummaryForm = () => {
type="submit"
className=" h-12 rounded-xl border bg-blue-500 px-3 py-1.5 font-satoshi text-white hover:bg-blue-400"
>
Send
Send.
</button>
</div>
</form>
Expand Down
88 changes: 88 additions & 0 deletions frontend/src/services/parsing/ParseWithSource.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Link } from "react-router-dom";
import { EmbeddingInfo } from "./chat";

interface ParseStringWithLinksProps {
text: string;
chunkData: EmbeddingInfo[];
}
const ParseStringWithLinks: React.FC<ParseStringWithLinksProps> = ({
text,
chunkData,
}) => {
const parseText = (text: string) => {
// Regular expression to find ***[GUID, Page X, Chunk Y]***
const regex = /\*\*\*\[([^\]]+)\]\*\*\*/g;

// Create a map of chunk information for easy lookup
const chunkMap = new Map<
string,
{ name: string; page_number: number; chunk_number: number; text: string }
>();
chunkData.forEach(({ file_id, name, page_number, chunk_number, text }) => {
const key = `${file_id}-${page_number}-${chunk_number}`;
chunkMap.set(key, { name, page_number, chunk_number, text });
// console.log(
// `Set chunkMap[${key}] = { name: ${name}, page_number: ${page_number}, chunk_number: ${chunk_number}, text: ${text} }`
// );
});

// console.log("chunkMap:", chunkMap);

// Use replace method to process the text and insert links
const processedText = text.split(regex).map((part, index) => {
// If the index is odd, it means this part is between *** ***
if (index % 2 === 1) {
// Extract GUID, page number, and chunk number from the matched part
const guidMatch = part.match(/([a-f0-9\-]{36})/);
const pageNumberMatch = part.match(/Page\s*(?:Number:)?\s*(\d+)/i);
const chunkNumberMatch = part.match(/Chunk\s*(\d+)/i);

// console.log("Matched Part:", part);
// console.log("GUID Match:", guidMatch);
// console.log("Page Number Match:", pageNumberMatch);
// console.log("Chunk Number Match:", chunkNumberMatch);

if (guidMatch && pageNumberMatch && chunkNumberMatch) {
const guid = guidMatch[1];
const pageNumber = pageNumberMatch[1];
const chunkNumber = chunkNumberMatch[1];

const chunkKey = `${guid}-${pageNumber}-${chunkNumber}`;
const chunkData = chunkMap.get(chunkKey);

if (chunkData) {
const { name, text: chunkText } = chunkData;
const tooltipContent = `Document Name: ${name}, Page: ${pageNumber}, Chunk: ${chunkNumber}, Text: ${chunkText}`;
// console.log("chunkKey:", chunkKey);
// console.log("tooltipContent:", tooltipContent);

return (
<Link
key={index}
className="cursor-pointer rounded-xl bg-gray-100 px-1.5 py-0.5 text-[13px] font-medium text-gray-400 hover:bg-gray-200"
to={`/PromptChatManager?fileguid=${guid}&page=${pageNumber}`}
title={tooltipContent}
>
{`${pageNumber}`}
</Link>
);
}
}
}

return (
<span
key={index}
dangerouslySetInnerHTML={{ __html: part.replace(/\n/g, "<br />") }}
/>
);
});

// console.log("processedText:", processedText);
return <>{processedText}</>;
};
// console.log("processedText:", <div>{parseText(text)}</div>);
return <div>{parseText(text)}</div>;
};

export default ParseStringWithLinks;
37 changes: 37 additions & 0 deletions frontend/src/services/parsing/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export interface ChatLogItem {
type: string;
message: string | SearchResult;
}

export interface SearchResult {
question: string;
llm_response: string;
embeddings_info: EmbeddingInfo[];
}

export interface GetAllPrompts {
id: number | string;
guid: string;
PromptText: string;
IsActive: null | boolean;
Area: string;
CreatedAt: Date;
LastModified: Date;
}

export interface EmbeddingInfo {
name: string;
text: string;
chunk_number: number;
file_id: number;
page_number: number;
distance: number;
}

export interface ChatLog {
type: string;
message: string;
userId?: number;
first_name?: string;
timestamp: string;
}

0 comments on commit 86fb52b

Please sign in to comment.