mirror of https://github.com/hwchase17/langchain
templates: Ionic Shopping Assistant (#16648)
- **Description:** This is a template for creating shopping assistant chat bots - **Issue:** Example for creating a shopping assistant with OpenAI Tools Agent - **Dependencies:** Ionic https://github.com/ioniccommerce/ionic_langchain - **Twitter handle:** @ioniccommerce --------- Co-authored-by: Erick Friis <erick@langchain.dev>erick/release-notes
parent
7237dc67d4
commit
7c6a2a8384
@ -0,0 +1,69 @@
|
||||
# shopping-assistant
|
||||
|
||||
This template creates a shopping assistant that helps users find products that they are looking for.
|
||||
|
||||
This template will use `Ionic` to search for products.
|
||||
|
||||
## Environment Setup
|
||||
|
||||
This template will use `OpenAI` by default.
|
||||
Be sure that `OPENAI_API_KEY` is set in your environment.
|
||||
|
||||
## Usage
|
||||
|
||||
To use this package, you should first have the LangChain CLI installed:
|
||||
|
||||
```shell
|
||||
pip install -U langchain-cli
|
||||
```
|
||||
|
||||
To create a new LangChain project and install this as the only package, you can do:
|
||||
|
||||
```shell
|
||||
langchain app new my-app --package shopping-assistant
|
||||
```
|
||||
|
||||
If you want to add this to an existing project, you can just run:
|
||||
|
||||
```shell
|
||||
langchain app add shopping-assistant
|
||||
```
|
||||
|
||||
And add the following code to your `server.py` file:
|
||||
```python
|
||||
from shopping_assistant.agent import agent_executor as shopping_assistant_chain
|
||||
|
||||
add_routes(app, shopping_assistant_chain, path="/shopping-assistant")
|
||||
```
|
||||
|
||||
(Optional) Let's now configure LangSmith.
|
||||
LangSmith will help us trace, monitor and debug LangChain applications.
|
||||
LangSmith is currently in private beta, you can sign up [here](https://smith.langchain.com/).
|
||||
If you don't have access, you can skip this section
|
||||
|
||||
|
||||
```shell
|
||||
export LANGCHAIN_TRACING_V2=true
|
||||
export LANGCHAIN_API_KEY=<your-api-key>
|
||||
export LANGCHAIN_PROJECT=<your-project> # if not specified, defaults to "default"
|
||||
```
|
||||
|
||||
If you are inside this directory, then you can spin up a LangServe instance directly by:
|
||||
|
||||
```shell
|
||||
langchain serve
|
||||
```
|
||||
|
||||
This will start the FastAPI app with a server is running locally at
|
||||
[http://localhost:8000](http://localhost:8000)
|
||||
|
||||
We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
|
||||
We can access the playground at [http://127.0.0.1:8000/shopping-assistant/playground](http://127.0.0.1:8000/shopping-assistant/playground)
|
||||
|
||||
We can access the template from code with:
|
||||
|
||||
```python
|
||||
from langserve.client import RemoteRunnable
|
||||
|
||||
runnable = RemoteRunnable("http://localhost:8000/shopping-assistant")
|
||||
```
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,31 @@
|
||||
[tool.poetry]
|
||||
name = "shopping-assistant"
|
||||
version = "0.0.1"
|
||||
description = "A template for a shopping assistant agent"
|
||||
authors = []
|
||||
readme = "README.md"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.8.12,<4.0"
|
||||
langchain = "^0.1"
|
||||
openai = "<2"
|
||||
ionic-langchain = "^0.2.2"
|
||||
langchain-openai = "^0.0.5"
|
||||
langchainhub = "^0.1"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
langchain-cli = ">=0.0.20"
|
||||
|
||||
[tool.langserve]
|
||||
export_module = "shopping_assistant.agent"
|
||||
export_attr = "agent_executor"
|
||||
|
||||
[tool.templates-hub]
|
||||
use-case = "chatbot"
|
||||
author = "LangChain"
|
||||
integrations = ["Ionic"]
|
||||
tags = ["conversation", "agents"]
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
@ -0,0 +1,47 @@
|
||||
from typing import List, Tuple
|
||||
|
||||
from ionic_langchain.tool import IonicTool
|
||||
from langchain.agents import AgentExecutor, create_openai_tools_agent
|
||||
from langchain_core.messages import AIMessage, SystemMessage
|
||||
from langchain_core.prompts import (
|
||||
ChatPromptTemplate,
|
||||
HumanMessagePromptTemplate,
|
||||
MessagesPlaceholder,
|
||||
)
|
||||
from langchain_core.pydantic_v1 import BaseModel, Field
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
tools = [IonicTool().tool()]
|
||||
|
||||
llm = ChatOpenAI(temperature=0.5, model_name="gpt-3.5-turbo-1106", streaming=True)
|
||||
|
||||
# You can modify these!
|
||||
AI_CONTENT = """
|
||||
I should use the full pdp url that the tool provides me.
|
||||
Always include query parameters
|
||||
"""
|
||||
SYSTEM_CONTENT = """
|
||||
You are a shopping assistant.
|
||||
You help humans find the best product given their {input}.
|
||||
"""
|
||||
messages = [
|
||||
SystemMessage(content=SYSTEM_CONTENT),
|
||||
HumanMessagePromptTemplate.from_template("{input}"),
|
||||
AIMessage(content=AI_CONTENT),
|
||||
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
||||
]
|
||||
|
||||
prompt = ChatPromptTemplate.from_messages(messages)
|
||||
agent = create_openai_tools_agent(llm, tools, prompt)
|
||||
|
||||
|
||||
class AgentInput(BaseModel):
|
||||
input: str
|
||||
chat_history: List[Tuple[str, str]] = Field(
|
||||
..., extra={"widget": {"type": "chat", "input": "input", "output": "output"}}
|
||||
)
|
||||
|
||||
|
||||
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True).with_types(
|
||||
input_type=AgentInput
|
||||
)
|
Loading…
Reference in New Issue