forked from docker/genai-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
2,503 additions
and
2 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 |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
!*.py | ||
!requirements.txt | ||
!images/* | ||
!front-end/* | ||
front-end/node_modules/* |
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,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" ] |
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,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}) |
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,11 @@ | ||
FROM node:alpine | ||
|
||
WORKDIR /app | ||
|
||
COPY front-end/ . | ||
|
||
RUN npm install | ||
|
||
EXPOSE 8505 | ||
|
||
ENTRYPOINT [ "npm", "run", "dev" ] |
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,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? |
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,3 @@ | ||
{ | ||
"recommendations": ["svelte.svelte-vscode"] | ||
} |
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,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) | ||
``` |
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,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> |
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,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"] | ||
} |
Oops, something went wrong.