Update SQL templates (#12464)

This commit is contained in:
Lance Martin
2023-10-27 16:34:37 -07:00
committed by GitHub
parent a476147189
commit f10c17c6a4
14 changed files with 204 additions and 28 deletions

View File

@@ -27,10 +27,3 @@ You can select other files and specify their download path (browse [here](https:
This template includes an example DB of 2023 NBA rosters.
You can see instructions to build this DB [here](https://github.com/facebookresearch/llama-recipes/blob/main/demo_apps/StructuredLlama.ipynb).
## Installation
```bash
# from inside your LangServe instance
poe add sql/llama2-ollama
```

View File

@@ -0,0 +1,54 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a0314df0-da99-4086-a96f-b14df05b3362",
"metadata": {},
"source": [
"## Run Template\n",
"\n",
"In `server.py`, set -\n",
"```\n",
"add_routes(app, chain, path=\"/sql_llamacpp\")\n",
"```\n",
"\n",
"This template includes an example DB of 2023 NBA rosters.\n",
"\n",
"We can ask questions related to NBA players. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff5869c6-2065-48f3-bb43-52a515968276",
"metadata": {},
"outputs": [],
"source": [
"from langserve.client import RemoteRunnable\n",
"sql_app = RemoteRunnable('http://0.0.0.0:8001/sql_llamacpp')\n",
"sql_app.invoke({\"question\": \"What team is Klay Thompson on?\"})"
]
}
],
"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.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -1,3 +1,3 @@
from llamacpp.chain import chain
from sql_llamacpp.chain import chain
__all__ = ["chain"]

View File

@@ -6,6 +6,7 @@ import requests
from langchain.llms import LlamaCpp
from langchain.memory import ConversationBufferMemory
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.pydantic_v1 import BaseModel
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnableLambda, RunnablePassthrough
from langchain.utilities import SQLDatabase
@@ -113,9 +114,12 @@ prompt_response = ChatPromptTemplate.from_messages(
("human", template),
]
)
# Supply the input types to the prompt
class InputType(BaseModel):
question: str
chain = (
RunnablePassthrough.assign(query=sql_response_memory)
RunnablePassthrough.assign(query=sql_response_memory).with_types(input_type=InputType)
| RunnablePassthrough.assign(
schema=get_schema,
response=lambda x: db.run(x["query"]),