-
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.
- Loading branch information
1 parent
5e59246
commit dc48c2e
Showing
64 changed files
with
16,949 additions
and
6 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,64 @@ | ||
#!/usr/bin/env python | ||
"""Example of a simple chatbot that just passes current conversation | ||
state back and forth between server and client. | ||
""" | ||
from typing import List, Union | ||
|
||
from fastapi import FastAPI | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from langchain.chat_models import ChatAnthropic | ||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage | ||
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder | ||
|
||
from langserve import add_routes | ||
from langserve.pydantic_v1 import BaseModel, Field | ||
|
||
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=["*"], | ||
) | ||
|
||
|
||
# Declare a chain | ||
prompt = ChatPromptTemplate.from_messages( | ||
[ | ||
("system", "You are a helpful, professional assistant named Cob."), | ||
MessagesPlaceholder(variable_name="messages"), | ||
] | ||
) | ||
|
||
chain = prompt | ChatAnthropic(model="claude-2") | ||
|
||
|
||
class InputChat(BaseModel): | ||
"""Input for the chat endpoint.""" | ||
|
||
messages: List[Union[HumanMessage, AIMessage, SystemMessage]] = Field( | ||
..., | ||
description="The chat messages representing the current conversation.", | ||
) | ||
|
||
|
||
add_routes( | ||
app, | ||
chain.with_types(input_type=InputChat), | ||
enable_feedback_endpoint=True, | ||
enable_public_trace_link_endpoint=True, | ||
) | ||
|
||
if __name__ == "__main__": | ||
import uvicorn | ||
|
||
uvicorn.run(app, host="localhost", port=8000) |
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,98 @@ | ||
import json | ||
import mimetypes | ||
import os | ||
from string import Template | ||
from typing import Sequence, Type | ||
|
||
from fastapi.responses import Response | ||
from langchain.schema.runnable import Runnable | ||
|
||
from langserve.pydantic_v1 import BaseModel | ||
|
||
|
||
class ChatPlaygroundTemplate(Template): | ||
delimiter = "____" | ||
|
||
|
||
def _get_mimetype(path: str) -> str: | ||
"""Get mimetype for file. | ||
Custom implementation of mimetypes.guess_type that | ||
uses the file extension to determine the mimetype for some files. | ||
This is necessary due to: https://bugs.python.org/issue43975 | ||
Resolves issue: https://github.com/langchain-ai/langserve/issues/245 | ||
Args: | ||
path (str): Path to file | ||
Returns: | ||
str: Mimetype of file | ||
""" | ||
try: | ||
file_extension = path.lower().split(".")[-1] | ||
except IndexError: | ||
return mimetypes.guess_type(path)[0] | ||
|
||
if file_extension == "js": | ||
return "application/javascript" | ||
elif file_extension == "css": | ||
return "text/css" | ||
elif file_extension in ["htm", "html"]: | ||
return "text/html" | ||
|
||
# If the file extension is not one of the specified ones, | ||
# use the default guess method | ||
mime_type = mimetypes.guess_type(path)[0] | ||
return mime_type | ||
|
||
|
||
async def serve_chat_playground( | ||
runnable: Runnable, | ||
input_schema: Type[BaseModel], | ||
config_keys: Sequence[str], | ||
base_url: str, | ||
file_path: str, | ||
feedback_enabled: bool, | ||
public_trace_link_enabled: bool, | ||
) -> Response: | ||
"""Serve the playground.""" | ||
local_file_path = os.path.abspath( | ||
os.path.join( | ||
os.path.dirname(__file__), | ||
"./chat_playground/dist", | ||
file_path or "index.html", | ||
) | ||
) | ||
|
||
base_dir = os.path.abspath( | ||
os.path.join(os.path.dirname(__file__), "./chat_playground/dist") | ||
) | ||
|
||
if base_dir != os.path.commonpath((base_dir, local_file_path)): | ||
return Response("Not Found", status_code=404) | ||
try: | ||
with open(local_file_path, encoding="utf-8") as f: | ||
mime_type = _get_mimetype(local_file_path) | ||
if mime_type in ("text/html", "text/css", "application/javascript"): | ||
response = ChatPlaygroundTemplate(f.read()).substitute( | ||
LANGSERVE_BASE_URL=base_url[1:] | ||
if base_url.startswith("/") | ||
else base_url, | ||
LANGSERVE_CONFIG_SCHEMA=json.dumps( | ||
runnable.config_schema(include=config_keys).schema() | ||
), | ||
LANGSERVE_INPUT_SCHEMA=json.dumps(input_schema.schema()), | ||
LANGSERVE_FEEDBACK_ENABLED=json.dumps( | ||
"true" if feedback_enabled else "false" | ||
), | ||
LANGSERVE_PUBLIC_TRACE_LINK_ENABLED=json.dumps( | ||
"true" if public_trace_link_enabled else "false" | ||
), | ||
) | ||
else: | ||
response = f.buffer.read() | ||
except FileNotFoundError: | ||
return Response("Not Found", status_code=404) | ||
|
||
return Response(response, media_type=mime_type) |
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,18 @@ | ||
module.exports = { | ||
root: true, | ||
env: { browser: true, es2020: true }, | ||
extends: [ | ||
'eslint:recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
'plugin:react-hooks/recommended', | ||
], | ||
ignorePatterns: ['dist', '.eslintrc.cjs'], | ||
parser: '@typescript-eslint/parser', | ||
plugins: ['react-refresh'], | ||
rules: { | ||
'react-refresh/only-export-components': [ | ||
'warn', | ||
{ allowConstantExport: true }, | ||
], | ||
}, | ||
} |
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,28 @@ | ||
# 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? | ||
|
||
.yarn | ||
|
||
!dist |
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 @@ | ||
# React + TypeScript + Vite | ||
|
||
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. | ||
|
||
Currently, two official plugins are available: | ||
|
||
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh | ||
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh | ||
|
||
## Expanding the ESLint configuration | ||
|
||
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules: | ||
|
||
- Configure the top-level `parserOptions` property like this: | ||
|
||
```js | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
project: ['./tsconfig.json', './tsconfig.node.json'], | ||
tsconfigRootDir: __dirname, | ||
}, | ||
``` | ||
|
||
- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked` | ||
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked` | ||
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.