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

@@ -15,11 +15,4 @@ Also follow instructions to download your LLM of interest:
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-ollama
```
You can see instructions to build this DB [here](https://github.com/facebookresearch/llama-recipes/blob/main/demo_apps/StructuredLlama.ipynb).

View File

@@ -0,0 +1,54 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d55f5fd9-21eb-433d-9259-0a588d9197c0",
"metadata": {},
"source": [
"## Run Template\n",
"\n",
"In `server.py`, set -\n",
"```\n",
"add_routes(app, chain, path=\"/sql_ollama\")\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": "50c27e82-92d8-4fa1-8bc4-b6544e59773d",
"metadata": {},
"outputs": [],
"source": [
"from langserve.client import RemoteRunnable\n",
"sql_app = RemoteRunnable('http://0.0.0.0:8001/sql_ollama')\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

@@ -3,6 +3,7 @@ from pathlib import Path
from langchain.chat_models import ChatOllama
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
@@ -82,8 +83,12 @@ prompt_response = ChatPromptTemplate.from_messages(
]
)
# 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"]),