-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples for widgets for playground --------- Co-authored-by: Bagatur <[email protected]> Co-authored-by: Nuno Campos <[email protected]> Co-authored-by: Tat Dat Duong <[email protected]>
- Loading branch information
1 parent
44dc693
commit 17eda1e
Showing
7 changed files
with
218 additions
and
58 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import base64 | ||
from typing import Any, Dict, List, Tuple | ||
|
||
from fastapi import FastAPI | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from langchain.document_loaders.blob_loaders import Blob | ||
from langchain.document_loaders.parsers.pdf import PDFMinerParser | ||
from langchain.schema.runnable import RunnableLambda | ||
from pydantic import BaseModel, Field | ||
|
||
from langserve.server import add_routes | ||
|
||
app = FastAPI( | ||
title="LangChain Server", | ||
version="1.0", | ||
description="Spin up a simple api server using Langchain's Runnable interfaces", | ||
) | ||
|
||
|
||
# Set all CORS enabled origins | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
expose_headers=["*"], | ||
) | ||
|
||
|
||
class ChatHistory(BaseModel): | ||
chat_history: List[Tuple[str, str]] = Field( | ||
..., | ||
examples=[[("a", "aa")]], | ||
extra={"widget": {"type": "chat", "input": "question", "output": "answer"}}, | ||
) | ||
question: str | ||
|
||
|
||
class FileProcessingRequest(BaseModel): | ||
file: bytes = Field(..., extra={"widget": {"type": "base64file"}}) | ||
num_chars: int = 100 | ||
|
||
|
||
def chat_with_bot(input: Dict[str, Any]) -> Dict[str, Any]: | ||
"""Bot that repeats the question twice.""" | ||
return { | ||
"answer": input["question"] * 2, | ||
"woof": "its so bad to woof, meow is better", | ||
} | ||
|
||
|
||
def process_file(input: Dict[str, Any]) -> str: | ||
"""Extract the text from the first page of the PDF.""" | ||
content = base64.decodebytes(input["file"]) | ||
blob = Blob(data=content) | ||
documents = list(PDFMinerParser().lazy_parse(blob)) | ||
content = documents[0].page_content | ||
return content[: input["num_chars"]] | ||
|
||
|
||
add_routes( | ||
app, | ||
RunnableLambda(chat_with_bot).with_types(input_type=ChatHistory), | ||
config_keys=["configurable"], | ||
path="/chat", | ||
) | ||
|
||
add_routes( | ||
app, | ||
RunnableLambda(process_file).with_types(input_type=FileProcessingRequest), | ||
config_keys=["configurable"], | ||
path="/pdf", | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
import uvicorn | ||
|
||
uvicorn.run(app, host="localhost", port=8000) |
110 changes: 55 additions & 55 deletions
110
.../playground/dist/assets/index-0ea7a041.js → .../playground/dist/assets/index-d732a22e.js
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,44 @@ | ||
import { ChangeEvent } from "react"; | ||
import { withJsonFormsControlProps } from "@jsonforms/react"; | ||
import { rankWith, and, schemaMatches, isControl } from "@jsonforms/core"; | ||
import { isJsonSchemaExtra } from "../utils/schema"; | ||
|
||
export const fileBase64Tester = rankWith( | ||
12, | ||
and( | ||
isControl, | ||
schemaMatches((schema) => { | ||
if (!isJsonSchemaExtra(schema)) return false; | ||
return schema.extra.widget.type === "base64file"; | ||
}) | ||
) | ||
); | ||
|
||
export const FileBase64ControlRenderer = withJsonFormsControlProps((props) => { | ||
const handleFileUpload = (event: ChangeEvent<HTMLInputElement>) => { | ||
const file = event.target.files?.[0]; | ||
if (!file) return; | ||
|
||
const reader = new FileReader(); | ||
|
||
reader.onload = () => { | ||
const base64String = reader.result as string | null; | ||
if (base64String != null) { | ||
const prefix = base64String.indexOf("base64,") + "base64,".length; | ||
props.handleChange(props.path, base64String.slice(prefix)); | ||
} | ||
}; | ||
|
||
reader.readAsDataURL(file); | ||
}; | ||
|
||
return ( | ||
<div className="control"> | ||
<label className="text-xs uppercase font-semibold text-ls-gray-100"> | ||
{props.label} | ||
</label> | ||
|
||
<input type="file" onChange={handleFileUpload} /> | ||
</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,27 @@ | ||
import { JsonSchema } from "@jsonforms/core"; | ||
|
||
type JsonSchemaExtra = JsonSchema & { | ||
extra: { | ||
widget: { | ||
type: string; | ||
}; | ||
}; | ||
}; | ||
|
||
export function isJsonSchemaExtra(x: JsonSchema): x is JsonSchemaExtra { | ||
if (!("extra" in x && typeof x.extra === "object" && x.extra != null)) { | ||
return false; | ||
} | ||
|
||
if ( | ||
!( | ||
"widget" in x.extra && | ||
typeof x.extra.widget === "object" && | ||
x.extra.widget != null | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |