This is the Repo for InteRecAgent, a interactive recommender agent, which applies Large Language Model(LLM) to bridge the gap between traditional recommender systems and conversational recommender system(CRS).
Figure 1: InteRecAgent Framework
InteRecAgent (Interactive Recommender Agent) is a framework to utilize pre-trained domain-specific recommendation tools (such as SQL tools, id-based recommendation models) and large language models (LLM) to implement an interactive, conversational recommendation agent. In this framework, the LLM primarily engages in user interaction and parses user interests as input for the recommendation tools, which are responsible for finding suitable items.
Within the InteRecAgent framework, recommendation tools are divided into three main categories: query, retrieval, and ranking. You need to provide the API for the LLM and the pre-configured domain-specific recommendation tools to build an interactive recommendation agent using the InteRecAgent framework. Neither the LLM nor the recommendation tools will be updated or modified within InteRecAgent.
This repository mainly implements the right-hand side of the figure, i.e., the communication between the LLM and the recommendation tools. For more details, please refer to our paper Recommender AI Agent: Integrating Large Language Models for Interactive Recommendations.
InteRecAgent consists of 4 necessary components:
-
LLM: A large language model, which serves as conversational agent.
-
Item profile table: A table containing item informations, whose columns consists of id, title, tag, description, price, release_date, visited_num, et al.
-
Query module: A SQL module to query item information in the item profile table.
- Input: SQL command
- Output: information queried with SQL
-
Retrieval module: The module aims to retrieve item candidates from the all item corups according to user's intention(requirements). Note that the module does not function in deal with user's personal profile, like user history, user age, et al. Instead, it focuses on what user wants, like "give me some sports games", "I want some popular games". The module should consist of at least two kinds of retrieval tools:
-
SQL tool: The tools is used to deal with complex search condition, which is related to item information. For example, "I want some popular sports games". Then the tool would use SQL command to search in the item profile table.
- Input: SQL command
- Output: execution log
-
Item similarity tool: The tools aims to retrieve items according to item similarity. Sometimes, user's intention is not clear enough to organized as SQL command, for example, "I want some games similar with Call of Duty", where the requirements is expressed implicitly through item similarity instead of explicit item features.
- Input: item name list as string.
- Output: execution log
-
-
Ranking module: Refine the rank of item candidates according to schema (popularity, similarity, preference). User prefernece comprises
preferandunwanted. The module could be a traditional recommender model, which inputs user and item features and outputs relevant score.- Input: ranking schema, user's profile (prefer, unwanted)
- Output: execution log
-
Environments
First, install other packages listed in
requirements.txtcd InteRecAgent conda create -n interecagent python==3.9 conda activate interecagent pip install -r requirements.txt -
Prepare data resource
For your convenience, a volunteer (thanks Xu Huang for the contribution!) has prepared a copy of ready-to-run data resources based on public dataset. Please download the resources from GoogleDrive / RecDrive. After you unzip the all_resources.zip file, you will see three folders, corresponding to three domains. Create a folder named "resources" under "InteRecAgent" and copy the data resource to it. For example, the folder structure is like "./InteRecAgent/resources/game".
-
How to run
-
If you are using personal OpenAI API, please setup those environments:
export OPENAI_API_KEY="xxxx" export API_TYPE="open_ai"
-
If you are using Azure OpenAI API, please setup those environments:
export OPENAI_API_KEY="xxx" export OPENAI_API_BASE="xxx" export OPENAI_API_VERSION="xxx" export OPENAI_API_TYPE="azure"
Then, you can launch the app with:
DOMAIN=game python app.py --engine gpt-4
Note that
DOMAINrepresents the item domain, e.g.,game,movieandbeauty_productin the provided data sources. Replacegpt-4with your own deploy name when using Azure OpenAI API.We also provide a shell script
run.sh, where commonly used arguments are given. You could directly set the API related information inrun.sh, or create a new shell scriptoai.shthat would be loaded inrun.sh. GPT-4 API is highly recommended for the InteRecAgent since it has remarkable instruction-following capability.Here is an example of the
oai.shscript:API_KEY="xxxxxx" # your api key API_BASE="https://xxxx.azure.com/" # [https://xxxx.azure.com, https://api.openai.com/v1] API_VERSION="2023-03-15-preview" API_TYPE="azure" # ['open_ai', 'azure'] engine="gpt4" # model name for OpenAI or deployment name for Azure OpenAI. GPT-4 is recommended. bot_type="chat" # model type, ["chat", "completetion"]. For gpt-3.5-turbo and gpt-4, it should be "chat". For text-davinci-003, it should be "completetion"
Meanwhile, we support to serve local models such as Vicuna or other opensource models as the backbone language models instead of OpenAI APIs:
-
Download the weights of Vicuna from huggingface.
-
Install
fschatpackage according to FastChat -
Deploy the Vicuna model with OpenAI-Compatible RESTful APIs by running the following command:
# First, launch the controller python3 -m fastchat.serve.controller # Then, launch the model worker(s) python3 -m fastchat.serve.model_worker --model-path path-to-vicuna # Finally, launch the RESTful API server python3 -m fastchat.serve.openai_api_server --host localhost --port 8000
-
Set the configuration for local model API in
run.shAPI_KEY="EMPTY" API_BASE="http://localhost:8000/v1" API_VERSION="2023-03-15-preview" API_TYPE="open_ai" engine="vicuna-7b-v1.5" # model name bot_type="completetion" # model type
-
Run the
run.shscript to start the chatbot.
-
-
Features
There are several optional features to enhance the agent.
-
History Shortening
enable_shorten: if true, enable shortening chat history by LLM; else use all chat history
-
Demonstrations Selector
demo_mode: mode to choose demonstration. Optional values: [zero,fixed,dynamic]. Ifzero, no demonstration would be used (zero-shot). Iffixed, the firstdemo_numsexamples indemo_dir_or_filewould be used. Ifdynamic, mostdemo_numsrelated examples would be selected for in-context learning.demo_dir_or_file: the directory or file path of demostrations. If a directory path is given, all.jsonlfile in the folder would be loaded. Ifdemo_modeiszero, the argument is invalid. Default None.demo_nums: number of demonstrations used in prompt for in-context learning. The argument is invalid whendemo_diris not given. Default 3.
-
Reflection
enable_reflection: if true, enable reflection for better plan makingreflection_limits: maximum times of reflection
Note that reflection happens after the plan is finished. That means the conversational agent would generate an answer first and then do reflection.
-
Plan First
plan_first: if true, the agent would make tool using plan first. There would only a tool executor for LLM to call, where the plan is input. Default true.
Additionally, we have implemented a version without using the black-box API calling in langchain. To enable it, use the following arguments.
langchain: if true, use langchain in plan-first strategy. Otherwise, the API calls would be made by directly using openai. Default false.
-
Please cite the following paper if the project helps you in anyway:
@article{10.1145/3731446,
author = {Huang, Xu and Lian, Jianxun and Lei, Yuxuan and Yao, Jing and Lian, Defu and Xie, Xing},
title = {Recommender AI Agent: Integrating Large Language Models for Interactive Recommendations},
year = {2025},
publisher = {Association for Computing Machinery},
address = {New York, NY, USA},
issn = {1046-8188},
url = {https://doi.org/10.1145/3731446},
doi = {10.1145/3731446},
note = {Just Accepted},
journal = {ACM Trans. Inf. Syst.},
month = apr,
keywords = {Conversational Recommendation, Large Language Models, Interactive Agents}
}