Skip to content
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

demo: sample streaming request to API and documents extract #3

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_X_API_KEY=...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ dist-ssr
*.njsproj
*.sln
*.sw?

.env
11 changes: 10 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { ChatExample } from "./ChatExample";
import { DocumentExtractExample } from "./DocumentExtractExample";

export function App() {
return <h1>BärGPT - KI Testumgebung</h1>;
return (
<div className="items-left mx-auto flex w-full max-w-[800px] flex-col gap-8">
<h1>BärGPT - KI Testumgebung</h1>
<ChatExample></ChatExample>
<DocumentExtractExample></DocumentExtractExample>
</div>
);
}
75 changes: 75 additions & 0 deletions src/ChatExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { useState } from "react";

export function ChatExample() {
const [chatMessage, setChatMessage] = useState("");

async function streamToString(body: ReadableStream<Uint8Array>) {
setChatMessage("");
const reader = body?.pipeThrough(new TextDecoderStream()).getReader();
while (reader) {
const stream = await reader.read();
if (stream.done) {
break;
}
const chunks = stream.value
.toString()
.replace(/^data: /gm, "")
.split("\n")
.filter((c: string) => Boolean(c.length) && c !== "[DONE]")
.map((c: string) => JSON.parse(c));
if (chunks) {
for (const chunk of chunks) {
const content = chunk.choices[0].delta.content;
if (!content) {
continue;
}
setChatMessage((prev) => prev + content);
}
}
}
}

return (
<div>
<button
className="rounded bg-blue-500 px-4 py-2 font-bold text-white hover:bg-blue-700"
onClick={async () => {
setChatMessage("");
try {
const response = await fetch(
`https://ber-gpt-backend.onrender.com/chat`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": import.meta.env.VITE_X_API_KEY,
llm: "azure",
},
body: JSON.stringify({
messages: [
{
role: "user",
content: "Wer bist du? Antworte ausführlich.",
},
],
}),
},
);

if (!response.body) {
throw new Error("Response body is empty");
}
streamToString(response.body);
} catch (err) {
console.error(err);
}
}}
>
Example API Call to /chat
</button>
{chatMessage && (
<p className="mt-8 rounded-md bg-red-100 p-2">{chatMessage}</p>
)}
</div>
);
}
50 changes: 50 additions & 0 deletions src/DocumentExtractExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { FormEvent, useState } from "react";

export function DocumentExtractExample() {
const [extractedText, setExtractedText] = useState("");

const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();

const formData = new FormData();
formData.append("file", event.currentTarget.file.files[0]);

const response = await fetch(
"https://ber-gpt-backend.onrender.com/documents/extract",
{
method: "POST",
headers: {
"x-api-key": import.meta.env.VITE_X_API_KEY,
},
body: formData,
},
);

if (response.ok) {
const data = await response.json();
setExtractedText(data.content);
} else {
console.error("Failed to extract text from document");
}
};

return (
<div>
<form
onSubmit={handleSubmit}
className="flex flex-row items-center gap-4"
>
<button
type="submit"
className="rounded bg-blue-500 px-4 py-2 font-bold text-white hover:bg-blue-700"
>
Example API Call to /documents/extract
</button>
<input type="file" name="file" />
</form>
{extractedText && (
<p className="mt-8 rounded-md bg-green-100 p-2">{extractedText}</p>
)}
</div>
);
}