Skip to content

Commit ed9dc56

Browse files
author
Erick Friis
committed
projct
0 parents  commit ed9dc56

File tree

9 files changed

+2020
-0
lines changed

9 files changed

+2020
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

Dockerfile

Lines changed: 21 additions & 0 deletions
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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# perplexaty
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/chain.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
2+
from langchain_core.runnables import (
3+
RunnableLambda,
4+
RunnableParallel,
5+
RunnablePassthrough,
6+
)
7+
from langchain_exa import ExaSearchRetriever
8+
from langchain_openai import ChatOpenAI
9+
10+
retriever = ExaSearchRetriever(k=3, highlights=True)
11+
12+
document_prompt = PromptTemplate.from_template(
13+
"""
14+
<source>
15+
<url>{url}</url>
16+
<highlights>{highlights}</highlights>
17+
</source>
18+
"""
19+
)
20+
21+
document_chain = (
22+
RunnableLambda(
23+
lambda document: {
24+
"highlights": document.metadata["highlights"],
25+
"url": document.metadata["url"],
26+
}
27+
)
28+
| document_prompt
29+
)
30+
31+
retrieval_chain = (
32+
retriever | document_chain.map() | (lambda docs: "\n".join([i.text for i in docs]))
33+
)
34+
35+
36+
generation_prompt = ChatPromptTemplate.from_messages(
37+
[
38+
(
39+
"system",
40+
"You are an expert research assistant. You use xml-formatted context to research people's questions.",
41+
),
42+
(
43+
"human",
44+
"""
45+
Please answer the following query based on the provided context. Please cite your sources at the end of your response.:
46+
47+
Query: {query}
48+
---
49+
<context>
50+
{context}
51+
</context>
52+
""",
53+
),
54+
]
55+
)
56+
57+
llm = ChatOpenAI()
58+
59+
chain = (
60+
RunnableParallel(
61+
{
62+
"query": RunnablePassthrough(),
63+
"context": retrieval_chain,
64+
}
65+
)
66+
| generation_prompt
67+
| llm
68+
).with_types(input_type=str)

app/server.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from fastapi import FastAPI
2+
from fastapi.responses import RedirectResponse
3+
from langserve import add_routes
4+
from app.chain 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+
# Edit this to add the chain you want to add
15+
add_routes(app, chain, path="/search")
16+
17+
if __name__ == "__main__":
18+
import uvicorn
19+
20+
uvicorn.run(app, host="0.0.0.0", port=8000)

packages/README.md

Whitespace-only changes.

poetry.lock

Lines changed: 1805 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[tool.poetry]
2+
name = "perplexaty"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["Your Name <[email protected]>"]
6+
readme = "README.md"
7+
packages = [
8+
{ include = "app" },
9+
]
10+
11+
[tool.poetry.dependencies]
12+
python = "^3.11"
13+
uvicorn = "^0.23.2"
14+
langserve = {extras = ["server"], version = ">=0.0.30"}
15+
pydantic = "<2"
16+
langchain-core = "^0.1.15"
17+
langchain-openai = "^0.0.3"
18+
langchain-exa = {git = "https://github.com/langchain-ai/langchain.git", rev = "erick/partner-exa", subdirectory = "libs/partners/exa"}
19+
20+
21+
[tool.poetry.group.dev.dependencies]
22+
langchain-cli = ">=0.0.15"
23+
24+
[build-system]
25+
requires = ["poetry-core"]
26+
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)