-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ⚡ override setup method use lanarky favicon * 🐛 fix stream_response * ✨ add `system` param * ✨ add StreamingClient * 📝 update getting started doc * 🔧 update nav * 🔥 remove print * 📝 minor update * 📝 add boilerplate doc structure * ✨ add WebSocketClient 🔧 update dependencies * 📝 add streaming and websocket docs * ✨ add stream_response to WebsocketClient * 📝 update openai adapter docs ⚡ send END event for openai websocket factory endpoint 🔧 update navigation features 📝 update openai api router doc * 📝 complete openai adapter docs - 🔧 update nav - 📝 update langchain adapter docs * 🔧 update css for mobile devices * 🐛 fix `build_factory_websocket_endpoint` in langchain adapter * 📝 complete langchain adapter docs * 🔥 remove deployment section will be added in future release
- Loading branch information
Showing
27 changed files
with
1,383 additions
and
89 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[run] | ||
omit = | ||
lanarky/clients.py | ||
lanarky/adapters/*/__init__.py |
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,14 @@ | ||
# Adapters | ||
|
||
The **Adapters API** allows Lanarky users to build microservices using popular LLM frameworks. | ||
|
||
We will cover the following adapters in depth: | ||
|
||
- [OpenAI](./openai/index.md): build microservices using the | ||
[OpenAI Python SDK](https://platform.openai.com/docs/api-reference?lang=python) | ||
- [LangChain](./langchain/index.md): build microservices using the | ||
[LangChain](https://www.langchain.com/) | ||
|
||
!!! note "Note from Author" | ||
|
||
The **Adapters API** is still in active development. I will add more adapters in the future. |
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,48 @@ | ||
Lanarky offers a collection of callback handlers for LangChain. These callback | ||
handlers are useful in executing intermediate callback events related to your LangChain | ||
microservice. | ||
|
||
Lanarky offers callback handlers for both streaming and WebSockets. We will take a look at | ||
both of them in this guide. | ||
|
||
!!! note | ||
|
||
All callback handlers can be imported from the `lanarky.adapters.langchain.callbacks` | ||
module. | ||
|
||
## Tokens | ||
|
||
- `TokenStreamingCallbackHandler`: handles streaming of the intermediate tokens over HTTP | ||
- `TokenWebSocketCallbackHandler`: handles streaming of the intermediate tokens over WebSockets | ||
|
||
Both callback handlers offer token streaming in two modes: `text` and `json`. In `text` mode, | ||
the callback handlers will use raw token string as event data. In `json` mode, the callback | ||
handlers will use a JSON object containing the token string as event data. | ||
|
||
These callback handlers are useful for all chains where the `llm` component supports streaming. | ||
|
||
## Source Documents | ||
|
||
- `SourceDocumentStreamingCallbackHandler`: handles streaming of the source documents | ||
over HTTP | ||
- `SourceDocumentWebSocketCallbackHandler`: handles streaming of the source documents | ||
over WebSockets | ||
|
||
The source documents are sent at the end of a chain execution as a `source_documents` event. | ||
|
||
These callback handlers are useful for retrieval-based chains like `RetrievalQA`. | ||
|
||
## Agents | ||
|
||
- `FinalTokenStreamingCallbackHandler`: handles streaming of the final answer tokens over HTTP | ||
- `FinalTokenWebSocketCallbackHandler`: handles streaming of the final answer tokens over WebSockets | ||
|
||
Both callback handlers are extension of the token streaming callback handlers where the tokens are | ||
streamed only when the LLM agent has reached the final step of its execution. | ||
|
||
These callback handlers are useful for all agent types like `ZeroShotAgent`. | ||
|
||
!!! note | ||
|
||
The callback handlers also inherit some functionality of the `FinalStreamingStdOutCallbackHandler` | ||
callback handler. Check out [LangChain Docs](https://api.python.langchain.com/en/latest/callbacks/langchain.callbacks.streaming_stdout_final_only.FinalStreamingStdOutCallbackHandler.html) to know more. |
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,103 @@ | ||
--- | ||
hide: | ||
- toc | ||
--- | ||
|
||
FastAPI offers a powerful [Dependency Injection](https://fastapi.tiangolo.com/tutorial/dependencies/) | ||
system that allows you to inject dependencies into your API endpoints. Lanarky extends this functionality | ||
by offering LangChain as a dependency. | ||
|
||
!!! example "Experimental" | ||
|
||
LLM-based dependency injection is an experimental feature. We will add more functionality | ||
based on community feedback and viable use cases. If you have ideas or suggestions, we | ||
would love to hear from you. Feel free to open an issue on | ||
[GitHub](https://github.com/ajndkr/lanarky/issues/new/choose). | ||
|
||
Let's take a look at how we can use LangChain as a dependency. | ||
|
||
```python | ||
import os | ||
|
||
from langchain.chains import LLMChain | ||
from langchain.chat_models import ChatOpenAI | ||
from langchain.prompts import ( | ||
ChatPromptTemplate, | ||
HumanMessagePromptTemplate, | ||
PromptTemplate, | ||
) | ||
|
||
from lanarky import Lanarky | ||
from lanarky.adapters.langchain.dependencies import Depends | ||
|
||
os.environ["OPENAI_API_KEY"] = "add-your-openai-api-key-here" | ||
|
||
|
||
app = Lanarky() | ||
|
||
|
||
def chain_factory(temperature: float = 0.0, verbose: bool = False) -> LLMChain: | ||
return LLMChain( | ||
llm=ChatOpenAI(temperature=temperature), | ||
prompt=ChatPromptTemplate.from_messages( | ||
[ | ||
HumanMessagePromptTemplate( | ||
prompt=PromptTemplate.from_template("Respond in JSON: {input}") | ||
), | ||
] | ||
), | ||
verbose=verbose, | ||
) | ||
|
||
|
||
@app.post("/") | ||
async def endpoint(outputs: dict = Depends(chain_factory)): | ||
return outputs["text"] | ||
``` | ||
|
||
In the above example, we pass `chain_factory` as a dependency to the endpoint. The endpoint | ||
exposes the dependency function arguments as query parameters. This allows us to configure | ||
the dependency at runtime. | ||
|
||
To test the above endpoint, let's create a client script: | ||
|
||
```python | ||
import click | ||
import httpx | ||
|
||
|
||
@click.command() | ||
@click.option("--input", required=True) | ||
def main(input: str): | ||
url = "http://localhost:8000/" | ||
|
||
with httpx.Client() as client: | ||
response = client.post(url, json={"input": input}) | ||
if response.status_code == 200: | ||
data = response.json() | ||
print(f"Received: {data}") | ||
else: | ||
print(response.text) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
``` | ||
|
||
First, start the server: | ||
|
||
```bash | ||
uvicorn app:app | ||
``` | ||
|
||
Then, run the client script: | ||
|
||
<!-- termynal --> | ||
|
||
``` | ||
$ python client.py --input "Who won the world series in 2020?" | ||
Received: { | ||
"team": "Los Angeles Dodgers", | ||
"year": 2020 | ||
} | ||
``` |
Oops, something went wrong.