-
Notifications
You must be signed in to change notification settings - Fork 231
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
5 changed files
with
194 additions
and
4 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
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,105 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Client\n", | ||
"\n", | ||
"Demo of a client interacting with a remote agent. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from langserve import RemoteRunnable\n", | ||
"\n", | ||
"remote_runnable = RemoteRunnable(\"http://localhost:8000/\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Remote runnable has the same interface as local runnables" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"{'output': 'Hello! How can I assist you today?'}" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"await remote_runnable.ainvoke({\"input\": \"hi!\"})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"{'output': 'Eugene thinks that cats like fish.'}" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"remote_runnable.invoke({\"input\": \"what does eugene think of cats?\"})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
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,82 @@ | ||
#!/usr/bin/env python | ||
"""Example LangChain server exposes a conversational retrieval chain.""" | ||
from fastapi import FastAPI | ||
from langchain.agents import AgentExecutor, tool | ||
from langchain.agents.format_scratchpad import format_to_openai_functions | ||
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser | ||
from langchain.chat_models import ChatOpenAI | ||
from langchain.embeddings import OpenAIEmbeddings | ||
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder | ||
from langchain.tools.render import format_tool_to_openai_function | ||
from langchain.vectorstores import FAISS | ||
from pydantic import BaseModel | ||
|
||
from langserve import add_routes | ||
|
||
vectorstore = FAISS.from_texts( | ||
["cats like fish", "dogs like sticks"], embedding=OpenAIEmbeddings() | ||
) | ||
retriever = vectorstore.as_retriever() | ||
|
||
|
||
@tool | ||
def get_eugene_thoughts(query: str) -> list: | ||
"""Returns Eugene's thoughts on a topic.""" | ||
return retriever.get_relevant_documents(query) | ||
|
||
|
||
tools = [get_eugene_thoughts] | ||
|
||
prompt = ChatPromptTemplate.from_messages( | ||
[ | ||
("system", "You are a helpful assistant."), | ||
("user", "{input}"), | ||
MessagesPlaceholder(variable_name="agent_scratchpad"), | ||
] | ||
) | ||
|
||
llm = ChatOpenAI() | ||
|
||
llm_with_tools = llm.bind(functions=[format_tool_to_openai_function(t) for t in tools]) | ||
|
||
agent = ( | ||
{ | ||
"input": lambda x: x["input"], | ||
"agent_scratchpad": lambda x: format_to_openai_functions( | ||
x["intermediate_steps"] | ||
), | ||
} | ||
| prompt | ||
| llm_with_tools | ||
| OpenAIFunctionsAgentOutputParser() | ||
) | ||
|
||
agent_executor = AgentExecutor(agent=agent, tools=tools) | ||
|
||
app = FastAPI( | ||
title="LangChain Server", | ||
version="1.0", | ||
description="Spin up a simple api server using Langchain's Runnable interfaces", | ||
) | ||
|
||
|
||
# We need to add these input/output schemas because the current AgentExecutor | ||
# is lacking in schemas. | ||
class Input(BaseModel): | ||
input: str | ||
|
||
|
||
class Output(BaseModel): | ||
output: str | ||
|
||
|
||
# Adds routes to the app for using the chain under: | ||
# /invoke | ||
# /batch | ||
# /stream | ||
add_routes(app, agent_executor, input_type=Input, output_type=Output) | ||
|
||
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