Skip to content

Commit 05bbf94

Browse files
authored
LLaMA2 with JSON schema support template (#12435)
1 parent 134f085 commit 05bbf94

File tree

8 files changed

+1359
-1
lines changed

8 files changed

+1359
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Extraction with LLaMA2 Function Calling
2+
3+
This template shows how to do extraction of structured data from unstructured data, using LLaMA2 [fine-tuned for grammars and jsonschema](https://replicate.com/andreasjansson/llama-2-13b-chat-gguf).
4+
5+
Specify the scehma you want to extract in `chain.py`
6+
7+
By default, it will extract the title and author of papers.
8+
9+
## LLM
10+
11+
This template will use `Replicate` [hosted version](https://replicate.com/andreasjansson/llama-2-13b-chat-gguf) of LLaMA.
12+
13+
Be sure that `REPLICATE_API_TOKEN` is set in your environment.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "9faf648c-541e-4368-82a8-96287dbf34de",
6+
"metadata": {},
7+
"source": [
8+
"## Document Loading\n",
9+
"\n",
10+
"Load a blog post on agents."
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 1,
16+
"id": "662a843a-49e8-40ec-bd32-0f44bc4159a1",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"from langchain.document_loaders import WebBaseLoader\n",
21+
"loader = WebBaseLoader(\"https://lilianweng.github.io/posts/2023-06-23-agent/\")\n",
22+
"text = loader.load()"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"id": "67306dbd-d79c-4723-825e-7d88edb811ba",
28+
"metadata": {},
29+
"source": [
30+
"## Run Template\n",
31+
"\n",
32+
"In `server.py`, set -\n",
33+
"```\n",
34+
"add_routes(app, chain_ext, path=\"/llama2_functions\")\n",
35+
"```"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"id": "3668ba4b-254e-4a3b-bfb5-53242572cb1b",
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"from langserve.client import RemoteRunnable\n",
46+
"llama2_function = RemoteRunnable('http://0.0.0.0:8001/llama2_functions')"
47+
]
48+
}
49+
],
50+
"metadata": {
51+
"kernelspec": {
52+
"display_name": "Python 3 (ipykernel)",
53+
"language": "python",
54+
"name": "python3"
55+
},
56+
"language_info": {
57+
"codemirror_mode": {
58+
"name": "ipython",
59+
"version": 3
60+
},
61+
"file_extension": ".py",
62+
"mimetype": "text/x-python",
63+
"name": "python",
64+
"nbconvert_exporter": "python",
65+
"pygments_lexer": "ipython3",
66+
"version": "3.9.16"
67+
}
68+
},
69+
"nbformat": 4,
70+
"nbformat_minor": 5
71+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from llama2_functions.chain import chain
2+
3+
__all__ = ["chain"]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from langchain.chat_models import ChatOpenAI
2+
from langchain.llms import Replicate
3+
from langchain.prompts import ChatPromptTemplate
4+
5+
# LLM
6+
replicate_id = "andreasjansson/llama-2-13b-chat-gguf:60ec5dda9ff9ee0b6f786c9d1157842e6ab3cc931139ad98fe99e08a35c5d4d4" # noqa: E501
7+
model = Replicate(
8+
model=replicate_id,
9+
model_kwargs={"temperature": 0.8,
10+
"max_length": 500,
11+
"top_p": 0.95},
12+
)
13+
14+
# Prompt with output schema specification
15+
template = """A article will be passed to you. Extract from it all papers that are mentioned by this article.
16+
17+
Do not extract the name of the article itself. If no papers are mentioned that's fine - you don't need to extract any! Just return an empty list.
18+
19+
Do not make up or guess ANY extra information. Only extract what exactly is in the text.
20+
21+
Respond with json that adheres to the following jsonschema:
22+
23+
{{
24+
"$schema": "http://json-schema.org/draft-07/schema#",
25+
"type": "object",
26+
"properties": {{
27+
"author": {{
28+
"type": "string",
29+
"description": "The author of the paper."
30+
}},
31+
"title": {{
32+
"type": "string",
33+
"description": "The title of the paper."
34+
}}
35+
}},
36+
"required": ["author", "title"],
37+
"additionalProperties": false
38+
}}""" # noqa: E501
39+
40+
prompt = ChatPromptTemplate.from_messages([("system", template), ("human", "{input}")])
41+
42+
# Chain
43+
model = ChatOpenAI()
44+
chain = (
45+
prompt
46+
| model
47+
)

templates/llama2-functions/poetry.lock

Lines changed: 1204 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[tool.poetry]
2+
name = "llama2-functions"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["Lance Martin <lance@langchain.dev>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = ">=3.8.1,<4.0"
10+
langchain = ">=0.0.313, <0.1"
11+
replicate = ">=0.15.4"
12+
13+
[tool.langserve]
14+
export_module = "llama2_functions"
15+
export_attr = "chain"
16+
17+
[build-system]
18+
requires = ["poetry-core"]
19+
build-backend = "poetry.core.masonry.api"

templates/llama2-functions/tests/__init__.py

Whitespace-only changes.

templates/sql-llamacpp/sql_llamacpp/chain.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
# File name and URL
1414
file_name = "mistral-7b-instruct-v0.1.Q4_K_M.gguf"
15-
url = "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf"
15+
url = ("https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/"
16+
"mistral-7b-instruct-v0.1.Q4_K_M.gguf")
1617
# Check if file is present in the current directory
1718
if not os.path.exists(file_name):
1819
print(f"'{file_name}' not found. Downloading...")

0 commit comments

Comments
 (0)