├── .gitignore ├── .replit ├── README.md ├── app ├── __init__.py └── server.py ├── packages └── pirate-speak │ ├── LICENSE │ ├── README.md │ ├── pirate_speak │ ├── __init__.py │ └── chain.py │ ├── poetry.lock │ ├── pyproject.toml │ └── tests │ └── __init__.py ├── poetry.lock ├── pyproject.toml └── replit.nix /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | .python-version 3 | .venv -------------------------------------------------------------------------------- /.replit: -------------------------------------------------------------------------------- 1 | modules = ["python-3.10:v18-20230807-322e88b"] 2 | 3 | hidden = [".pythonlibs"] 4 | run = "langchain serve --host 0.0.0.0" 5 | onBoot = "pip install -U langchain-cli && poetry install" 6 | entrypoint = "main.py" 7 | 8 | [nix] 9 | channel = "stable-23_05" 10 | 11 | [unitTest] 12 | language = "python3" 13 | 14 | [deployment] 15 | run = ["sh", "-c", "langserve serve"] 16 | deploymentTarget = "cloudrun" 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🦜🔗 LangServe Replit Template 2 | 3 | [![Run on Repl.it](https://replit.com/badge/github/langchain-ai/langserve-replit-template)](https://replit.com/new/github/langchain-ai/langserve-replit-template) 4 | 5 | This template shows how to deploy a [LangChain Expression Language Runnable](https://python.langchain.com/docs/expression_language/) as a set of HTTP endpoints with stream and batch support using [LangServe](https://github.com/langchain-ai/langserve) onto [Replit](https://replit.com), a collaborative online code editor and platform for creating and deploying software. 6 | 7 | ## Getting started 8 | 9 | The default chat endpoint is a chain that translates questions into pirate dialect. 10 | 11 | 1. Deploy your app to Replit by [clicking here](https://replit.com/new/github/langchain-ai/langserve-replit-template). 12 | - You will also need to set an `OPENAI_API_KEY` environment variable by going under `Tools > Secrets` in the bottom left corner. 13 | - To enable tracing, you'll also need to set `LANGCHAIN_TRACING_V2=true`, `LANGCHAIN_API_KEY`, and optionally `LANGCHAIN_SESSION`. 14 | 2. Run `pip install -U langchain-cli` to install the required command. 15 | 3. Run `poetry install` to install the required dependencies. 16 | 4. Press `Run` on `main.py`. 17 | 5. Navigate to `https://your_url.repl.co/docs/` to see documentation for your live runnable, and `https://your_url.repl.co/pirate-speak/playground/` to access a playground where you can try sending requests! 18 | 19 | As you experiment, you can install the [LangSmith Replit extension](https://replit.com/extension/@langchain/a1d61149-f81e-4df7-9e91-69cfdcbccce3) to see traces of your runs in action by navigating to either your default project or the one set in your `LANGCHAIN_SESSION` environment variable. 20 | 21 | ## Calling from the client 22 | 23 | You can use the `RemoteRunnable` class in LangServe to call these hosted runnables: 24 | 25 | ```python 26 | from langserve import RemoteRunnable 27 | 28 | pirate_chain = RemoteRunnable("https://your_url.repl.co/pirate-speak/") 29 | 30 | pirate_chain.invoke({"question": "how are you?"}) 31 | 32 | # or async 33 | await pirate_chain.ainvoke({"question": "how are you?"}) 34 | 35 | # Supports astream 36 | async for msg in pirate_chain.astream({"question": "how are you?"}): 37 | print(msg, end="", flush=True) 38 | ``` 39 | 40 | In TypeScript (requires [LangChain.js](https://github.com/langchain-ai/langchainjs) version 0.0.166 or later): 41 | 42 | ```typescript 43 | import { RemoteRunnable } from "langchain/runnables/remote"; 44 | 45 | const pirateChain = new RemoteRunnable({ url: `https://your_url.repl.co/pirate-speak/` }); 46 | const result = await pirateChain.invoke({ 47 | "question": "how are you?", 48 | }); 49 | ``` 50 | 51 | You can also use `curl`: 52 | 53 | ```curl 54 | curl --location --request POST 'https://your_url.repl.co/pirate-speak/invoke' \ 55 | --header 'Content-Type: application/json' \ 56 | --data-raw '{ 57 | "input": { 58 | "question": "how are you?" 59 | } 60 | }' 61 | ``` 62 | 63 | ## Adding more chains 64 | 65 | You can add more chains from a variety of templates by using the LangChain CLI: 66 | 67 | `$ langchain app add