Skip to content

Commit 46cf42e

Browse files
author
Paul
committed
langchain openai tools agent chain using the new langserve playground
0 parents  commit 46cf42e

10 files changed

+2225
-0
lines changed

.env.example

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
OPENAI_API_KEY=sk-
2+
LANGCHAIN_API_KEY=
3+
LANGCHAIN_TRACING_V2=Tue
4+
LANGCHAIN_PROJECT=helloagent

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

Dockerfile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM python:3.11-slim
2+
3+
RUN pip install poetry==1.6.1
4+
5+
RUN poetry config virtualenvs.create false
6+
7+
WORKDIR /code
8+
9+
COPY ./pyproject.toml ./README.md ./poetry.lock* ./
10+
11+
COPY ./package[s] ./packages
12+
13+
RUN poetry install --no-interaction --no-ansi --no-root
14+
15+
COPY ./app ./app
16+
17+
RUN poetry install --no-interaction --no-ansi
18+
19+
EXPOSE 8080
20+
21+
CMD exec uvicorn app.server:app --host 0.0.0.0 --port 8080

README.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# helloagent
2+
3+
## Installation
4+
5+
Install the LangChain CLI if you haven't yet
6+
7+
```bash
8+
pip install -U langchain-cli
9+
```
10+
11+
## Adding packages
12+
13+
```bash
14+
# adding packages from
15+
# https://github.com/langchain-ai/langchain/tree/master/templates
16+
langchain app add $PROJECT_NAME
17+
18+
# adding custom GitHub repo packages
19+
langchain app add --repo $OWNER/$REPO
20+
# or with whole git string (supports other git providers):
21+
# langchain app add git+https://github.com/hwchase17/chain-of-verification
22+
23+
# with a custom api mount point (defaults to `/{package_name}`)
24+
langchain app add $PROJECT_NAME --api_path=/my/custom/path/rag
25+
```
26+
27+
Note: you remove packages by their api path
28+
29+
```bash
30+
langchain app remove my/custom/path/rag
31+
```
32+
33+
## Setup LangSmith (Optional)
34+
LangSmith will help us trace, monitor and debug LangChain applications.
35+
LangSmith is currently in private beta, you can sign up [here](https://smith.langchain.com/).
36+
If you don't have access, you can skip this section
37+
38+
39+
```shell
40+
export LANGCHAIN_TRACING_V2=true
41+
export LANGCHAIN_API_KEY=<your-api-key>
42+
export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"
43+
```
44+
45+
## Launch LangServe
46+
47+
```bash
48+
langchain serve
49+
```
50+
51+
## Running in Docker
52+
53+
This project folder includes a Dockerfile that allows you to easily build and host your LangServe app.
54+
55+
### Building the Image
56+
57+
To build the image, you simply:
58+
59+
```shell
60+
docker build . -t my-langserve-app
61+
```
62+
63+
If you tag your image with something other than `my-langserve-app`,
64+
note it for use in the next step.
65+
66+
### Running the Image Locally
67+
68+
To run the image, you'll need to include any environment variables
69+
necessary for your application.
70+
71+
In the below example, we inject the `OPENAI_API_KEY` environment
72+
variable with the value set in my local environment
73+
(`$OPENAI_API_KEY`)
74+
75+
We also expose port 8080 with the `-p 8080:8080` option.
76+
77+
```shell
78+
docker run -e OPENAI_API_KEY=$OPENAI_API_KEY -p 8080:8080 my-langserve-app
79+
```

app/__init__.py

Whitespace-only changes.

app/helloagent.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from langchain_openai import ChatOpenAI
2+
from dotenv import load_dotenv, find_dotenv
3+
from typing import List, Union
4+
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
5+
from langchain.pydantic_v1 import BaseModel, Field
6+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
7+
from langchain_community.tools import DuckDuckGoSearchRun
8+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, HumanMessagePromptTemplate
9+
from langchain.agents import create_openai_tools_agent, AgentExecutor
10+
from langchain_core.runnables import RunnableLambda
11+
12+
load_dotenv(find_dotenv())
13+
14+
prompt = ChatPromptTemplate.from_messages([
15+
SystemMessagePromptTemplate.from_template("You are a helpful assistant who makes fun of Swift developers."),
16+
MessagesPlaceholder(variable_name="chat_history", optional=True),
17+
HumanMessagePromptTemplate.from_template("{input}"),
18+
MessagesPlaceholder(variable_name="agent_scratchpad")
19+
])
20+
21+
gpt4 = ChatOpenAI(model="gpt-4-0125-preview", temperature=0)
22+
23+
agent_tools = [DuckDuckGoSearchRun()]
24+
langchain_agent = create_openai_tools_agent(gpt4, agent_tools, prompt)
25+
agent_executor = AgentExecutor(agent=langchain_agent, tools=agent_tools, return_intermediate_steps=True)
26+
27+
class Input(BaseModel):
28+
chat_history: List[Union[HumanMessage, AIMessage, SystemMessage]] = Field(default_factory=list)
29+
input: str
30+
31+
def parse_agent_output(agent_output):
32+
return agent_output["output"]
33+
34+
chain = (agent_executor | RunnableLambda(parse_agent_output)).with_types(input_type=Input, output_type=str)

app/server.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from fastapi import FastAPI
2+
from fastapi.responses import RedirectResponse
3+
from langserve import add_routes
4+
from app.helloagent import chain
5+
6+
app = FastAPI()
7+
8+
9+
@app.get("/")
10+
async def redirect_root_to_docs():
11+
return RedirectResponse("/docs")
12+
13+
14+
add_routes(app, chain, path="/helloagent", playground_type="chat")
15+
16+
if __name__ == "__main__":
17+
import uvicorn
18+
19+
uvicorn.run(app, host="0.0.0.0", port=8000)

packages/README.md

Whitespace-only changes.

0 commit comments

Comments
 (0)