This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
forked from LangStream/langstream
-
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.
[agents] Add Agent to call LangServe (langserve-invoke) (LangStream#673)
- Loading branch information
Showing
28 changed files
with
1,410 additions
and
20 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Invoking a LangServe service | ||
|
||
This sample application explains how to invoke a LangServe service and leverage streaming capabilities. | ||
|
||
## Set up your LangServe environment | ||
|
||
Start you LangServe application, the example below is using the LangServe sample [application](https://github.com/langchain-ai/langserve) | ||
|
||
```python | ||
#!/usr/bin/env python | ||
from fastapi import FastAPI | ||
from langchain.prompts import ChatPromptTemplate | ||
from langchain.chat_models import ChatAnthropic, ChatOpenAI | ||
from langserve import add_routes | ||
|
||
|
||
app = FastAPI( | ||
title="LangChain Server", | ||
version="1.0", | ||
description="A simple api server using Langchain's Runnable interfaces", | ||
) | ||
|
||
model = ChatOpenAI() | ||
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}") | ||
add_routes( | ||
app, | ||
prompt | model, | ||
path="/chain", | ||
) | ||
|
||
if __name__ == "__main__": | ||
import uvicorn | ||
|
||
uvicorn.run(app, host="localhost", port=8000) | ||
``` | ||
|
||
## Configure you OpenAI API Key and run the application | ||
|
||
```bash | ||
export OPENAI_API_KEY=... | ||
pip install fastapi langserve langchain openai sse_starlette uvicorn | ||
python example.py | ||
``` | ||
|
||
The sample application is exposing a chain at http://localhost:8000/chain/stream and http://localhost:8000/chain/invoke. | ||
|
||
The application, running in docker, connects to http://host.docker.internal:8000/chain/stream | ||
|
||
LangStream sends an input like this: | ||
|
||
```json | ||
{ | ||
"input": { | ||
"topic": "cats" | ||
} | ||
} | ||
``` | ||
|
||
When "topic" is the topic of the joke you want to generate and it is taken from the user input. | ||
|
||
## Deploy the LangStream application | ||
``` | ||
./bin/langstream docker run test -app examples/applications/langserve-invoke | ||
``` | ||
|
||
## Interact with the application | ||
|
||
You can now interact with the application using the UI opening your browser at http://localhost:8092/ |
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,41 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Copyright DataStax, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from fastapi import FastAPI | ||
from langchain.prompts import ChatPromptTemplate | ||
from langchain.chat_models import ChatOpenAI | ||
from langserve import add_routes | ||
|
||
|
||
app = FastAPI( | ||
title="LangChain Server", | ||
version="1.0", | ||
description="A simple api server using Langchain's Runnable interfaces", | ||
) | ||
|
||
model = ChatOpenAI() | ||
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}") | ||
add_routes( | ||
app, | ||
prompt | model, | ||
path="/chain", | ||
) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# | ||
# | ||
# Copyright DataStax, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
gateways: | ||
- id: chat | ||
type: chat | ||
chat-options: | ||
answers-topic: streaming-answers-topic | ||
questions-topic: input-topic | ||
headers: | ||
- value-from-parameters: session-id |
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,41 @@ | ||
# | ||
# Copyright DataStax, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
topics: | ||
- name: "input-topic" | ||
creation-mode: create-if-not-exists | ||
- name: "output-topic" | ||
creation-mode: create-if-not-exists | ||
- name: "streaming-answers-topic" | ||
creation-mode: create-if-not-exists | ||
pipeline: | ||
- type: "langserve-invoke" | ||
input: input-topic | ||
output: output-topic | ||
id: step1 | ||
configuration: | ||
output-field: value.answer | ||
stream-to-topic: streaming-answers-topic | ||
stream-response-field: value | ||
min-chunks-per-message: 10 | ||
debug: false | ||
method: POST | ||
allow-redirects: true | ||
handle-cookies: false | ||
url: "http://host.docker.internal:8000/chain/stream" | ||
fields: | ||
- name: topic | ||
expression: "value" |
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
2 changes: 1 addition & 1 deletion
2
...urce/src/main/resources/META-INF/services/ai.langstream.api.runner.code.AgentCodeProvider
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 +1 @@ | ||
ai.langstream.agents.azureblobstorage.source.AzureBlobStorageSourceCodeProvider | ||
ai.langstream.agents.azureblobstorage.AzureBlobStorageSourceCodeProvider |
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
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
Oops, something went wrong.