mirror of
				https://github.com/docker/genai-stack.git
				synced 2024-08-30 16:49:54 +03:00 
			
		
		
		
	Add API container + static client
This commit is contained in:
		| @@ -2,3 +2,5 @@ | ||||
| !*.py | ||||
| !requirements.txt | ||||
| !images/* | ||||
| !front-end/* | ||||
| front-end/node_modules/* | ||||
|   | ||||
							
								
								
									
										21
									
								
								api.Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								api.Dockerfile
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| FROM langchain/langchain | ||||
|  | ||||
| WORKDIR /app | ||||
|  | ||||
| RUN apt-get update && apt-get install -y \ | ||||
|     build-essential \ | ||||
|     curl \ | ||||
|     software-properties-common \ | ||||
|     && rm -rf /var/lib/apt/lists/* | ||||
|  | ||||
| COPY requirements.txt . | ||||
|  | ||||
| RUN pip install --upgrade -r requirements.txt | ||||
|  | ||||
| COPY api.py . | ||||
| COPY utils.py . | ||||
| COPY chains.py . | ||||
|  | ||||
| HEALTHCHECK CMD curl --fail http://localhost:8504 | ||||
|  | ||||
| ENTRYPOINT [ "uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8504" ] | ||||
							
								
								
									
										146
									
								
								api.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										146
									
								
								api.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,146 @@ | ||||
| import os | ||||
|  | ||||
| from langchain.graphs import Neo4jGraph | ||||
| from dotenv import load_dotenv | ||||
| from utils import ( | ||||
|     create_vector_index, | ||||
|     BaseLogger, | ||||
| ) | ||||
| from chains import ( | ||||
|     load_embedding_model, | ||||
|     load_llm, | ||||
|     configure_llm_only_chain, | ||||
|     configure_qa_rag_chain, | ||||
| ) | ||||
| from fastapi import FastAPI, Depends | ||||
| from pydantic import BaseModel | ||||
| from langchain.callbacks.base import BaseCallbackHandler | ||||
| from threading import Thread | ||||
| from queue import Queue, Empty | ||||
| from collections.abc import Generator | ||||
| from sse_starlette.sse import EventSourceResponse | ||||
| from fastapi.middleware.cors import CORSMiddleware | ||||
| import json | ||||
|  | ||||
| load_dotenv(".env") | ||||
|  | ||||
| url = os.getenv("NEO4J_URI") | ||||
| username = os.getenv("NEO4J_USERNAME") | ||||
| password = os.getenv("NEO4J_PASSWORD") | ||||
| ollama_base_url = os.getenv("OLLAMA_BASE_URL") | ||||
| embedding_model_name = os.getenv("EMBEDDING_MODEL") | ||||
| llm_name = os.getenv("LLM") | ||||
| # Remapping for Langchain Neo4j integration | ||||
| os.environ["NEO4J_URL"] = url | ||||
|  | ||||
| embeddings, dimension = load_embedding_model( | ||||
|     embedding_model_name, | ||||
|     config={ollama_base_url: ollama_base_url}, | ||||
|     logger=BaseLogger(), | ||||
| ) | ||||
|  | ||||
| # if Neo4j is local, you can go to http://localhost:7474/ to browse the database | ||||
| neo4j_graph = Neo4jGraph(url=url, username=username, password=password) | ||||
| create_vector_index(neo4j_graph, dimension) | ||||
|  | ||||
| llm = load_llm( | ||||
|     llm_name, logger=BaseLogger(), config={"ollama_base_url": ollama_base_url} | ||||
| ) | ||||
|  | ||||
| llm_chain = configure_llm_only_chain(llm) | ||||
| rag_chain = configure_qa_rag_chain( | ||||
|     llm, embeddings, embeddings_store_url=url, username=username, password=password | ||||
| ) | ||||
|  | ||||
|  | ||||
| class QueueCallback(BaseCallbackHandler): | ||||
|     """Callback handler for streaming LLM responses to a queue.""" | ||||
|  | ||||
|     def __init__(self, q): | ||||
|         self.q = q | ||||
|  | ||||
|     def on_llm_new_token(self, token: str, **kwargs) -> None: | ||||
|         self.q.put(token) | ||||
|  | ||||
|     def on_llm_end(self, *args, **kwargs) -> None: | ||||
|         return self.q.empty() | ||||
|  | ||||
|  | ||||
| def stream(cb, q) -> Generator: | ||||
|     job_done = object() | ||||
|  | ||||
|     def task(): | ||||
|         x = cb() | ||||
|         q.put(job_done) | ||||
|  | ||||
|     t = Thread(target=task) | ||||
|     t.start() | ||||
|  | ||||
|     content = "" | ||||
|  | ||||
|     # Get each new token from the queue and yield for our generator | ||||
|     while True: | ||||
|         try: | ||||
|             next_token = q.get(True, timeout=1) | ||||
|             if next_token is job_done: | ||||
|                 break | ||||
|             content += next_token | ||||
|             yield next_token, content | ||||
|         except Empty: | ||||
|             continue | ||||
|  | ||||
|  | ||||
| app = FastAPI() | ||||
| origins = ["*"] | ||||
|  | ||||
| app.add_middleware( | ||||
|     CORSMiddleware, | ||||
|     allow_origins=origins, | ||||
|     allow_credentials=True, | ||||
|     allow_methods=["*"], | ||||
|     allow_headers=["*"], | ||||
| ) | ||||
|  | ||||
|  | ||||
| @app.get("/") | ||||
| async def root(): | ||||
|     return {"message": "Hello World"} | ||||
|  | ||||
|  | ||||
| class Question(BaseModel): | ||||
|     text: str | ||||
|     rag: bool = False | ||||
|  | ||||
|  | ||||
| @app.get("/query-stream") | ||||
| def qstream(question: Question = Depends()): | ||||
|     output_function = llm_chain | ||||
|     if question.rag: | ||||
|         output_function = rag_chain | ||||
|  | ||||
|     q = Queue() | ||||
|  | ||||
|     def cb(): | ||||
|         output_function( | ||||
|             {"question": question.text, "chat_history": []}, | ||||
|             callbacks=[QueueCallback(q)], | ||||
|         ) | ||||
|  | ||||
|     def generate(): | ||||
|         yield json.dumps({"init": True, "model": llm_name}) | ||||
|         for token, _ in stream(cb, q): | ||||
|             yield json.dumps({"token": token}) | ||||
|  | ||||
|     return EventSourceResponse(generate(), media_type="text/event-stream") | ||||
|  | ||||
|  | ||||
| @app.get("/query") | ||||
| async def ask(question: Question = Depends()): | ||||
|     output_function = llm_chain | ||||
|     if question.rag: | ||||
|         output_function = rag_chain | ||||
|     result = output_function( | ||||
|         {"question": question.text, "chat_history": []}, callbacks=[] | ||||
|     ) | ||||
|  | ||||
|     return json.dumps({"result": result["answer"], "model": llm_name}) | ||||
| @@ -71,6 +71,8 @@ services: | ||||
|           ignore: | ||||
|             - bot.py | ||||
|             - pdf_bot.py | ||||
|             - api.py | ||||
|             - front-end/ | ||||
|  | ||||
|  | ||||
|   bot: | ||||
| @@ -104,6 +106,8 @@ services: | ||||
|           ignore: | ||||
|             - loader.py | ||||
|             - pdf_bot.py | ||||
|             - api.py | ||||
|             - front-end/ | ||||
|  | ||||
|     ports: | ||||
|       - 8501:8501 | ||||
| @@ -138,9 +142,64 @@ services: | ||||
|           ignore: | ||||
|             - loader.py | ||||
|             - bot.py | ||||
|  | ||||
|             - api.py | ||||
|             - front-end/ | ||||
|     ports: | ||||
|       - 8503:8503 | ||||
|  | ||||
|   api: | ||||
|     build: | ||||
|       dockerfile: api.Dockerfile | ||||
|     volumes: | ||||
|       - $PWD/embedding_model:/embedding_model | ||||
|     environment: | ||||
|       - NEO4J_URI=${NEO4J_URI-neo4j://database:7687} | ||||
|       - NEO4J_PASSWORD=${NEO4J_PASSWORD-password} | ||||
|       - NEO4J_USERNAME=${NEO4J_USERNAME-neo4j} | ||||
|       - OPENAI_API_KEY=${OPENAI_API_KEY} | ||||
|       - OLLAMA_BASE_URL=${OLLAMA_BASE_URL-http://host.docker.internal:11434} | ||||
|       - LLM=${LLM-llama2} | ||||
|       - EMBEDDING_MODEL=${EMBEDDING_MODEL-sentence_transformer} | ||||
|       - LANGCHAIN_ENDPOINT=${LANGCHAIN_ENDPOINT-"https://api.smith.langchain.com"} | ||||
|       - LANGCHAIN_TRACING_V2=${LANGCHAIN_TRACING_V2-false} | ||||
|       - LANGCHAIN_PROJECT=${LANGCHAIN_PROJECT} | ||||
|       - LANGCHAIN_API_KEY=${LANGCHAIN_API_KEY} | ||||
|     networks: | ||||
|       - net | ||||
|     depends_on: | ||||
|       database: | ||||
|         condition: service_healthy | ||||
|     x-develop: | ||||
|       watch: | ||||
|         - action: rebuild | ||||
|           path: . | ||||
|           ignore: | ||||
|             - loader.py | ||||
|             - bot.py | ||||
|             - pdf_bot.py | ||||
|             - front-end/ | ||||
|     ports: | ||||
|       - 8504:8504 | ||||
|  | ||||
|   front-end: | ||||
|     build: | ||||
|       dockerfile: front-end.Dockerfile | ||||
|     x-develop: | ||||
|       watch: | ||||
|         - action: sync | ||||
|           path: ./front-end | ||||
|           target: /app | ||||
|           ignore: | ||||
|             - ./front-end/node_modules/ | ||||
|         - action: rebuild | ||||
|           path: ./front-end/package.json | ||||
|     depends_on: | ||||
|       api: | ||||
|         condition: service_healthy | ||||
|     networks: | ||||
|       - net | ||||
|     ports: | ||||
|       - 8505:8505 | ||||
|  | ||||
| networks: | ||||
|   net: | ||||
|   | ||||
							
								
								
									
										11
									
								
								front-end.Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								front-end.Dockerfile
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| FROM node:alpine | ||||
|  | ||||
| WORKDIR /app | ||||
|  | ||||
| COPY front-end/ . | ||||
|  | ||||
| RUN npm install | ||||
|  | ||||
| EXPOSE 8505 | ||||
|  | ||||
| ENTRYPOINT [ "npm", "run", "dev" ] | ||||
							
								
								
									
										24
									
								
								front-end/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								front-end/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| # Logs | ||||
| logs | ||||
| *.log | ||||
| npm-debug.log* | ||||
| yarn-debug.log* | ||||
| yarn-error.log* | ||||
| pnpm-debug.log* | ||||
| lerna-debug.log* | ||||
|  | ||||
| node_modules | ||||
| dist | ||||
| dist-ssr | ||||
| *.local | ||||
|  | ||||
| # Editor directories and files | ||||
| .vscode/* | ||||
| !.vscode/extensions.json | ||||
| .idea | ||||
| .DS_Store | ||||
| *.suo | ||||
| *.ntvs* | ||||
| *.njsproj | ||||
| *.sln | ||||
| *.sw? | ||||
							
								
								
									
										3
									
								
								front-end/.vscode/extensions.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								front-end/.vscode/extensions.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| { | ||||
|   "recommendations": ["svelte.svelte-vscode"] | ||||
| } | ||||
							
								
								
									
										47
									
								
								front-end/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								front-end/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| # Svelte + Vite | ||||
|  | ||||
| This template should help get you started developing with Svelte in Vite. | ||||
|  | ||||
| ## Recommended IDE Setup | ||||
|  | ||||
| [VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode). | ||||
|  | ||||
| ## Need an official Svelte framework? | ||||
|  | ||||
| Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more. | ||||
|  | ||||
| ## Technical considerations | ||||
|  | ||||
| **Why use this over SvelteKit?** | ||||
|  | ||||
| - It brings its own routing solution which might not be preferable for some users. | ||||
| - It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app. | ||||
|  | ||||
| This template contains as little as possible to get started with Vite + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project. | ||||
|  | ||||
| Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate. | ||||
|  | ||||
| **Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?** | ||||
|  | ||||
| Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information. | ||||
|  | ||||
| **Why include `.vscode/extensions.json`?** | ||||
|  | ||||
| Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project. | ||||
|  | ||||
| **Why enable `checkJs` in the JS template?** | ||||
|  | ||||
| It is likely that most cases of changing variable types in runtime are likely to be accidental, rather than deliberate. This provides advanced typechecking out of the box. Should you like to take advantage of the dynamically-typed nature of JavaScript, it is trivial to change the configuration. | ||||
|  | ||||
| **Why is HMR not preserving my local component state?** | ||||
|  | ||||
| HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/sveltejs/svelte-hmr/tree/master/packages/svelte-hmr#preservation-of-local-state). | ||||
|  | ||||
| If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR. | ||||
|  | ||||
| ```js | ||||
| // store.js | ||||
| // An extremely simple external store | ||||
| import { writable } from 'svelte/store' | ||||
| export default writable(0) | ||||
| ``` | ||||
							
								
								
									
										13
									
								
								front-end/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								front-end/index.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| <!doctype html> | ||||
| <html lang="en"> | ||||
|   <head> | ||||
|     <meta charset="UTF-8" /> | ||||
|     <link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||||
|     <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||||
|     <title>Vite + Svelte</title> | ||||
|   </head> | ||||
|   <body> | ||||
|     <div id="app"></div> | ||||
|     <script type="module" src="/src/main.js"></script> | ||||
|   </body> | ||||
| </html> | ||||
							
								
								
									
										32
									
								
								front-end/jsconfig.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								front-end/jsconfig.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| { | ||||
|   "compilerOptions": { | ||||
|     "moduleResolution": "bundler", | ||||
|     "target": "ESNext", | ||||
|     "module": "ESNext", | ||||
|     /** | ||||
|      * svelte-preprocess cannot figure out whether you have | ||||
|      * a value or a type, so tell TypeScript to enforce using | ||||
|      * `import type` instead of `import` for Types. | ||||
|      */ | ||||
|     "verbatimModuleSyntax": true, | ||||
|     "isolatedModules": true, | ||||
|     "resolveJsonModule": true, | ||||
|     /** | ||||
|      * To have warnings / errors of the Svelte compiler at the | ||||
|      * correct position, enable source maps by default. | ||||
|      */ | ||||
|     "sourceMap": true, | ||||
|     "esModuleInterop": true, | ||||
|     "skipLibCheck": true, | ||||
|     /** | ||||
|      * Typecheck JS in `.svelte` and `.js` files by default. | ||||
|      * Disable this if you'd like to use dynamic types. | ||||
|      */ | ||||
|     "checkJs": true | ||||
|   }, | ||||
|   /** | ||||
|    * Use global.d.ts instead of compilerOptions.types | ||||
|    * to avoid limiting type declarations. | ||||
|    */ | ||||
|   "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"] | ||||
| } | ||||
							
								
								
									
										1879
									
								
								front-end/package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										1879
									
								
								front-end/package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										22
									
								
								front-end/package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								front-end/package.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| { | ||||
|   "name": "bot-ui", | ||||
|   "private": true, | ||||
|   "version": "0.0.0", | ||||
|   "type": "module", | ||||
|   "scripts": { | ||||
|     "dev": "vite", | ||||
|     "build": "vite build", | ||||
|     "preview": "vite preview" | ||||
|   }, | ||||
|   "devDependencies": { | ||||
|     "@sveltejs/vite-plugin-svelte": "^2.4.2", | ||||
|     "autoprefixer": "^10.4.16", | ||||
|     "postcss": "^8.4.31", | ||||
|     "svelte": "^4.0.5", | ||||
|     "tailwindcss": "^3.3.3", | ||||
|     "vite": "^4.4.5" | ||||
|   }, | ||||
|   "dependencies": { | ||||
|     "svelte-markdown": "^0.4.0" | ||||
|   } | ||||
| } | ||||
							
								
								
									
										6
									
								
								front-end/postcss.config.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								front-end/postcss.config.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| export default { | ||||
|   plugins: { | ||||
|     tailwindcss: {}, | ||||
|     autoprefixer: {}, | ||||
|   }, | ||||
| } | ||||
							
								
								
									
										1
									
								
								front-end/public/vite.svg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								front-end/public/vite.svg
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg> | ||||
| After Width: | Height: | Size: 1.5 KiB | 
							
								
								
									
										155
									
								
								front-end/src/App.svelte
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										155
									
								
								front-end/src/App.svelte
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,155 @@ | ||||
| <script> | ||||
|     import { tick } from "svelte"; | ||||
|     import SvelteMarkdown from "svelte-markdown"; | ||||
|     import botImage from "./assets/images/bot.jpeg"; | ||||
|     import meImage from "./assets/images/me.jpeg"; | ||||
|     import MdLink from "./lib/MdLink.svelte"; | ||||
|  | ||||
|     let messages = []; | ||||
|     let ragMode = true; | ||||
|     let question = "How can I create a chatbot on top of my local PDF files using langchain?"; | ||||
|     let shouldAutoScroll = true; | ||||
|     let input; | ||||
|     let appState = "idle"; // or receiving | ||||
|     let senderImages = { bot: botImage, me: meImage }; | ||||
|  | ||||
|     async function send() { | ||||
|         if (!question.trim().length) { | ||||
|             return; | ||||
|         } | ||||
|         appState = "receiving"; | ||||
|         addMessage("me", question, ragMode); | ||||
|         const messageId = addMessage("bot", "", ragMode); | ||||
|         try { | ||||
|             const evt = new EventSource( | ||||
|                 `http://localhost:8504/query-stream?text=${encodeURI(question)}&rag=${ragMode}` | ||||
|             ); | ||||
|             question = ""; | ||||
|             evt.onmessage = (e) => { | ||||
|                 if (e.data) { | ||||
|                     const data = JSON.parse(e.data); | ||||
|                     if (data.init) { | ||||
|                         updateMessage(messageId, "", data.model); | ||||
|                         return; | ||||
|                     } | ||||
|                     updateMessage(messageId, data.token); | ||||
|                 } | ||||
|             }; | ||||
|             evt.onerror = (e) => { | ||||
|                 // Stream will end with an error | ||||
|                 // and we want to close the connection on end (otherwise it will keep reconnecting) | ||||
|                 evt.close(); | ||||
|             }; | ||||
|         } catch (e) { | ||||
|             updateMessage(messageId, "Error: " + e.message); | ||||
|         } finally { | ||||
|             appState = "idle"; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     function updateMessage(existingId, text, model = null) { | ||||
|         if (!existingId) { | ||||
|             return; | ||||
|         } | ||||
|         const existingIdIndex = messages.findIndex((m) => m.id === existingId); | ||||
|         if (existingIdIndex === -1) { | ||||
|             return; | ||||
|         } | ||||
|         messages[existingIdIndex].text += text; | ||||
|         if (model) { | ||||
|             messages[existingIdIndex].model = model; | ||||
|         } | ||||
|         messages = messages; | ||||
|     } | ||||
|  | ||||
|     function addMessage(from, text, rag) { | ||||
|         const newId = Math.random().toString(36).substring(2, 9); | ||||
|         const message = { id: newId, from, text, rag }; | ||||
|         messages = messages.concat([message]); | ||||
|         return newId; | ||||
|     } | ||||
|  | ||||
|     function scrollToBottom(node, _) { | ||||
|         const scroll = () => node.scrollTo({ top: node.scrollHeight }); | ||||
|         scroll(); | ||||
|         return { update: () => shouldAutoScroll && scroll() }; | ||||
|     } | ||||
|  | ||||
|     function scrolling(e) { | ||||
|         shouldAutoScroll = e.target.scrollTop + e.target.clientHeight > e.target.scrollHeight - 55; | ||||
|     } | ||||
|  | ||||
|     $: appState === "idle" && input && focus(input); | ||||
|     async function focus(node) { | ||||
|         await tick(); | ||||
|         node.focus(); | ||||
|     } | ||||
|     // send(); | ||||
| </script> | ||||
|  | ||||
| <main class="h-full text-sm bg-gradient-to-t from-indigo-100 bg-fixed overflow-hidden"> | ||||
|     <div on:scroll={scrolling} class="flex h-full flex-col py-12 overflow-y-auto" use:scrollToBottom={messages}> | ||||
|         <div class="w-4/5 mx-auto flex flex-col mb-32"> | ||||
|             {#each messages as message (message.id)} | ||||
|                 <div | ||||
|                     class="max-w-[80%] min-w-[40%] rounded-lg p-4 mb-4 overflow-x-auto bg-white border border-indigo-200" | ||||
|                     class:self-end={message.from === "me"} | ||||
|                     class:text-right={message.from === "me"} | ||||
|                 > | ||||
|                     <div class="flex flex-row items-start gap-2"> | ||||
|                         <div | ||||
|                             class:ml-auto={message.from === "me"} | ||||
|                             class="relative w-12 h-12 border border-indigo-200 rounded-lg flex justify-center items-center" | ||||
|                         > | ||||
|                             <img | ||||
|                                 src={senderImages[message.from]} | ||||
|                                 alt="" | ||||
|                                 class="w-12 h-12 absolute top-0 left-0 rounded-lg" | ||||
|                             /> | ||||
|                         </div> | ||||
|                         {#if message.from === "bot"} | ||||
|                             <div class="text-sm"> | ||||
|                                 <div>Model: {message.model ? message.model : ""}</div> | ||||
|                                 <div>RAG: {message.rag ? "Enabled" : "Disabled"}</div> | ||||
|                             </div> | ||||
|                         {/if} | ||||
|                     </div> | ||||
|                     <div class="mt-4"><SvelteMarkdown source={message.text} renderers={{ link: MdLink }} /></div> | ||||
|                 </div> | ||||
|             {/each} | ||||
|         </div> | ||||
|         <div class="text-sm w-full fixed bottom-16"> | ||||
|             <div class="shadow-lg bg-indigo-50 rounded-lg w-1/2 mx-auto"> | ||||
|                 <div class="rounded-t-lg px-4 py-2 font-light"> | ||||
|                     <div class="font-semibold">RAG mode</div> | ||||
|                     <div class=""> | ||||
|                         <label class="mr-2"> | ||||
|                             <input type="radio" bind:group={ragMode} value={false} /> Disabled | ||||
|                         </label> | ||||
|                         <label> | ||||
|                             <input type="radio" bind:group={ragMode} value={true} /> Enabled | ||||
|                         </label> | ||||
|                     </div> | ||||
|                 </div> | ||||
|                 <form class="rounded-md w-full bg-white p-2 m-0" on:submit|preventDefault={send}> | ||||
|                     <input | ||||
|                         disabled={appState === "receiving"} | ||||
|                         class="text-lg w-full bg-white focus:outline-none px-4" | ||||
|                         bind:value={question} | ||||
|                         bind:this={input} | ||||
|                         type="text" | ||||
|                     /> | ||||
|                 </form> | ||||
|             </div> | ||||
|         </div> | ||||
|     </div> | ||||
| </main> | ||||
|  | ||||
| <style> | ||||
|     :global(pre) { | ||||
|         @apply bg-gray-100 rounded-lg p-4 border border-indigo-200; | ||||
|     } | ||||
|     :global(code) { | ||||
|         @apply text-indigo-500; | ||||
|     } | ||||
| </style> | ||||
							
								
								
									
										28
									
								
								front-end/src/app.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								front-end/src/app.css
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| @tailwind base; | ||||
| @tailwind components; | ||||
| @tailwind utilities; | ||||
|  | ||||
| body { | ||||
|   margin: 0; | ||||
|   min-width: 320px; | ||||
|   height: 100dvh; | ||||
| } | ||||
|  | ||||
| #app { | ||||
|   height: 100%; | ||||
| } | ||||
|  | ||||
| pre { | ||||
|   line-height: 1; | ||||
|   background-color: rgb(241, 241, 241); | ||||
|   padding: 4px 8px 8px; | ||||
|   border: 1px solid #ccc; | ||||
|   overflow-x: auto; | ||||
|   margin: 0 4px; | ||||
|   border-radius: 2px; | ||||
| } | ||||
|  | ||||
| ol { | ||||
|   padding: 1em; | ||||
|   list-style: decimal; | ||||
| } | ||||
							
								
								
									
										
											BIN
										
									
								
								front-end/src/assets/images/bot.jpeg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								front-end/src/assets/images/bot.jpeg
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 107 KiB | 
							
								
								
									
										
											BIN
										
									
								
								front-end/src/assets/images/me.jpeg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								front-end/src/assets/images/me.jpeg
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 124 KiB | 
							
								
								
									
										1
									
								
								front-end/src/assets/svelte.svg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								front-end/src/assets/svelte.svg
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="26.6" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 308"><path fill="#FF3E00" d="M239.682 40.707C211.113-.182 154.69-12.301 113.895 13.69L42.247 59.356a82.198 82.198 0 0 0-37.135 55.056a86.566 86.566 0 0 0 8.536 55.576a82.425 82.425 0 0 0-12.296 30.719a87.596 87.596 0 0 0 14.964 66.244c28.574 40.893 84.997 53.007 125.787 27.016l71.648-45.664a82.182 82.182 0 0 0 37.135-55.057a86.601 86.601 0 0 0-8.53-55.577a82.409 82.409 0 0 0 12.29-30.718a87.573 87.573 0 0 0-14.963-66.244"></path><path fill="#FFF" d="M106.889 270.841c-23.102 6.007-47.497-3.036-61.103-22.648a52.685 52.685 0 0 1-9.003-39.85a49.978 49.978 0 0 1 1.713-6.693l1.35-4.115l3.671 2.697a92.447 92.447 0 0 0 28.036 14.007l2.663.808l-.245 2.659a16.067 16.067 0 0 0 2.89 10.656a17.143 17.143 0 0 0 18.397 6.828a15.786 15.786 0 0 0 4.403-1.935l71.67-45.672a14.922 14.922 0 0 0 6.734-9.977a15.923 15.923 0 0 0-2.713-12.011a17.156 17.156 0 0 0-18.404-6.832a15.78 15.78 0 0 0-4.396 1.933l-27.35 17.434a52.298 52.298 0 0 1-14.553 6.391c-23.101 6.007-47.497-3.036-61.101-22.649a52.681 52.681 0 0 1-9.004-39.849a49.428 49.428 0 0 1 22.34-33.114l71.664-45.677a52.218 52.218 0 0 1 14.563-6.398c23.101-6.007 47.497 3.036 61.101 22.648a52.685 52.685 0 0 1 9.004 39.85a50.559 50.559 0 0 1-1.713 6.692l-1.35 4.116l-3.67-2.693a92.373 92.373 0 0 0-28.037-14.013l-2.664-.809l.246-2.658a16.099 16.099 0 0 0-2.89-10.656a17.143 17.143 0 0 0-18.398-6.828a15.786 15.786 0 0 0-4.402 1.935l-71.67 45.674a14.898 14.898 0 0 0-6.73 9.975a15.9 15.9 0 0 0 2.709 12.012a17.156 17.156 0 0 0 18.404 6.832a15.841 15.841 0 0 0 4.402-1.935l27.345-17.427a52.147 52.147 0 0 1 14.552-6.397c23.101-6.006 47.497 3.037 61.102 22.65a52.681 52.681 0 0 1 9.003 39.848a49.453 49.453 0 0 1-22.34 33.12l-71.664 45.673a52.218 52.218 0 0 1-14.563 6.398"></path></svg> | ||||
| After Width: | Height: | Size: 1.9 KiB | 
							
								
								
									
										7
									
								
								front-end/src/lib/MdLink.svelte
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								front-end/src/lib/MdLink.svelte
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| <script> | ||||
|     export let href = ""; | ||||
|     export let title = ""; | ||||
|     export let text = ""; | ||||
| </script> | ||||
|  | ||||
| [<a {href} {title}>{text}</a>] | ||||
							
								
								
									
										8
									
								
								front-end/src/main.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								front-end/src/main.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| import './app.css' | ||||
| import App from './App.svelte' | ||||
|  | ||||
| const app = new App({ | ||||
|   target: document.getElementById('app'), | ||||
| }) | ||||
|  | ||||
| export default app | ||||
							
								
								
									
										2
									
								
								front-end/src/vite-env.d.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								front-end/src/vite-env.d.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| /// <reference types="svelte" /> | ||||
| /// <reference types="vite/client" /> | ||||
							
								
								
									
										7
									
								
								front-end/svelte.config.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								front-end/svelte.config.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| import { vitePreprocess } from '@sveltejs/vite-plugin-svelte' | ||||
|  | ||||
| export default { | ||||
|   // Consult https://svelte.dev/docs#compile-time-svelte-preprocess | ||||
|   // for more information about preprocessors | ||||
|   preprocess: vitePreprocess(), | ||||
| } | ||||
							
								
								
									
										12
									
								
								front-end/tailwind.config.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								front-end/tailwind.config.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| /** @type {import('tailwindcss').Config} */ | ||||
| export default { | ||||
|   content: [ | ||||
|     "./index.html", | ||||
|     "./src/**/*.{svelte,js,ts,jsx,tsx}", | ||||
|   ], | ||||
|   theme: { | ||||
|     extend: {}, | ||||
|   }, | ||||
|   plugins: [], | ||||
| } | ||||
|  | ||||
							
								
								
									
										11
									
								
								front-end/vite.config.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								front-end/vite.config.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| import { defineConfig } from 'vite' | ||||
| import { svelte } from '@sveltejs/vite-plugin-svelte' | ||||
|  | ||||
| // https://vitejs.dev/config/ | ||||
| export default defineConfig({ | ||||
|   server: { | ||||
|     host: '0.0.0.0', | ||||
|     port: 8505, | ||||
|   }, | ||||
|   plugins: [svelte()], | ||||
| }) | ||||
| @@ -6,5 +6,9 @@ neo4j | ||||
| streamlit | ||||
| sentence_transformers==2.2.2 | ||||
| Pillow | ||||
| fastapi | ||||
| PyPDF2 | ||||
| torch==2.0.1 | ||||
| torch==2.0.1 | ||||
| pydantic | ||||
| uvicorn | ||||
| sse-starlette | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Oskar Hane
					Oskar Hane