Skip to content

Commit

Permalink
Add Gradio helpers - part 9 (LLM notebooks) (#2360)
Browse files Browse the repository at this point in the history
Ticket: CVS-147626

Notebooks:

1. llm-agent-functioncall/llm-agent-functioncall-qwen.ipynb
2. llm-agent-react/llm-agent-react-langchain.ipynb
3. llm-chatbot/llm-chatbot-generate-api.ipynb
4. llm-chatbot/llm-chatbot.ipynb
5. llm-question-answering/llm-question-answering.ipynb
6. llm-rag-langchain/llm-rag-langchain.ipynb
7. llm-rag-llamaindex/llm-rag-llamaindex.ipynb
  • Loading branch information
yatarkan authored Sep 6, 2024
1 parent 1420f47 commit fd22e96
Show file tree
Hide file tree
Showing 14 changed files with 1,226 additions and 1,114 deletions.
133 changes: 133 additions & 0 deletions notebooks/llm-agent-functioncall/gradio_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
from pathlib import Path
import requests
from PIL import Image
from typing import List
from qwen_agent.llm.schema import CONTENT, ROLE, USER, Message
from qwen_agent.gui.utils import convert_history_to_chatbot
from qwen_agent.gui.gradio import gr, mgr
from qwen_agent.gui import WebUI


openvino_logo = "openvino_logo.png"
openvino_logo_url = "https://cdn-avatars.huggingface.co/v1/production/uploads/1671615670447-6346651be2dcb5422bcd13dd.png"

if not Path(openvino_logo).exists():
image = Image.open(requests.get(openvino_logo_url, stream=True).raw)
image.save(openvino_logo)


chatbot_config = {
"prompt.suggestions": [
"Based on current weather in London, show me a picture of Big Ben",
"What is OpenVINO ?",
"Create an image of pink cat",
"What is the weather like in New York now ?",
"How many people live in Canada ?",
],
"agent.avatar": openvino_logo,
"input.placeholder": "Please input your request here",
}


class OpenVINOUI(WebUI):
def request_cancel(self):
self.agent_list[0].llm.ov_model.request.cancel()

def clear_history(self):
return []

def add_text(self, _input, _chatbot, _history):
_history.append(
{
ROLE: USER,
CONTENT: [{"text": _input}],
}
)
_chatbot.append([_input, None])
yield gr.update(interactive=False, value=None), _chatbot, _history

def run(
self,
messages: List[Message] = None,
share: bool = False,
server_name: str = None,
server_port: int = None,
**kwargs,
):
self.run_kwargs = kwargs

with gr.Blocks(
theme=gr.themes.Soft(),
css=".disclaimer {font-variant-caps: all-small-caps;}",
) as self.demo:
gr.Markdown("""<h1><center>OpenVINO Qwen Agent </center></h1>""")
history = gr.State([])

with gr.Row():
with gr.Column(scale=4):
chatbot = mgr.Chatbot(
value=convert_history_to_chatbot(messages=messages),
avatar_images=[
self.user_config,
self.agent_config_list,
],
height=900,
avatar_image_width=80,
flushing=False,
show_copy_button=True,
)
with gr.Column():
input = gr.Textbox(
label="Chat Message Box",
placeholder="Chat Message Box",
show_label=False,
container=False,
)
with gr.Column():
with gr.Row():
submit = gr.Button("Submit", variant="primary")
stop = gr.Button("Stop")
clear = gr.Button("Clear")
with gr.Column(scale=1):
agent_interactive = self.agent_list[0]
capabilities = [key for key in agent_interactive.function_map.keys()]
gr.CheckboxGroup(
label="Tools",
value=capabilities,
choices=capabilities,
interactive=False,
)
with gr.Row():
gr.Examples(self.prompt_suggestions, inputs=[input], label="Click on any example and press the 'Submit' button")

input_promise = submit.click(
fn=self.add_text,
inputs=[input, chatbot, history],
outputs=[input, chatbot, history],
queue=False,
)
input_promise = input_promise.then(
self.agent_run,
[chatbot, history],
[chatbot, history],
)
input_promise.then(self.flushed, None, [input])
stop.click(
fn=self.request_cancel,
inputs=None,
outputs=None,
cancels=[input_promise],
queue=False,
)
clear.click(lambda: None, None, chatbot, queue=False).then(self.clear_history, None, history)

self.demo.load(None)

self.demo.launch(share=share, server_name=server_name, server_port=server_port)


def make_demo(bot):
return OpenVINOUI(
bot,
chatbot_config=chatbot_config,
)
154 changes: 13 additions & 141 deletions notebooks/llm-agent-functioncall/llm-agent-functioncall-qwen.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,12 @@
"\"torch>=2.1\" \\\n",
"\"datasets\" \\\n",
"\"accelerate\" \\\n",
"\"qwen-agent>=0.0.6\" \"transformers>=4.38.1\" \"gradio==4.21.0\", \"modelscope-studio>=0.4.0\" \"langchain>=0.2.3\" \"langchain-community>=0.2.4\" \"wikipedia\"\n",
"\"qwen-agent==0.0.7\" \"transformers>=4.38.1\" \"gradio==4.21.0\", \"modelscope-studio>=0.4.0\" \"langchain>=0.2.3\" \"langchain-community>=0.2.4\" \"wikipedia\"\n",
"%pip install -q --extra-index-url https://download.pytorch.org/whl/cpu \\\n",
"\"git+https://github.com/huggingface/optimum-intel.git\" \\\n",
"\"git+https://github.com/openvinotoolkit/nncf.git\""
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2ec33075",
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"from PIL import Image\n",
"\n",
"openvino_logo = \"openvino_log.png\"\n",
"url = \"https://cdn-avatars.huggingface.co/v1/production/uploads/1671615670447-6346651be2dcb5422bcd13dd.png\"\n",
"\n",
"image = Image.open(requests.get(url, stream=True).raw)\n",
"image.save(openvino_logo)"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand Down Expand Up @@ -589,7 +572,6 @@
],
"source": [
"from qwen_agent.agents import Assistant\n",
"from qwen_agent.gui import WebUI\n",
"\n",
"bot = Assistant(llm=llm_cfg, function_list=tools, name=\"OpenVINO Agent\")"
]
Expand All @@ -601,132 +583,21 @@
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"from qwen_agent.llm.schema import CONTENT, ROLE, USER, Message\n",
"from qwen_agent.gui.utils import convert_history_to_chatbot\n",
"from qwen_agent.gui.gradio import gr, mgr\n",
"\n",
"\n",
"class OpenVINOUI(WebUI):\n",
" def request_cancel(self):\n",
" self.agent_list[0].llm.ov_model.request.cancel()\n",
"\n",
" def clear_history(self):\n",
" return []\n",
"\n",
" def add_text(self, _input, _chatbot, _history):\n",
" _history.append(\n",
" {\n",
" ROLE: USER,\n",
" CONTENT: [{\"text\": _input}],\n",
" }\n",
" )\n",
" _chatbot.append([_input, None])\n",
" yield gr.update(interactive=False, value=None), _chatbot, _history\n",
"\n",
" def run(\n",
" self,\n",
" messages: List[Message] = None,\n",
" share: bool = False,\n",
" server_name: str = None,\n",
" server_port: int = None,\n",
" **kwargs,\n",
" ):\n",
" self.run_kwargs = kwargs\n",
"\n",
" with gr.Blocks(\n",
" theme=gr.themes.Soft(),\n",
" css=\".disclaimer {font-variant-caps: all-small-caps;}\",\n",
" ) as self.demo:\n",
" gr.Markdown(\"\"\"<h1><center>OpenVINO Qwen Agent </center></h1>\"\"\")\n",
" history = gr.State([])\n",
"\n",
" with gr.Row():\n",
" with gr.Column(scale=4):\n",
" chatbot = mgr.Chatbot(\n",
" value=convert_history_to_chatbot(messages=messages),\n",
" avatar_images=[\n",
" self.user_config,\n",
" self.agent_config_list,\n",
" ],\n",
" height=900,\n",
" avatar_image_width=80,\n",
" flushing=False,\n",
" show_copy_button=True,\n",
" )\n",
" with gr.Column():\n",
" input = gr.Textbox(\n",
" label=\"Chat Message Box\",\n",
" placeholder=\"Chat Message Box\",\n",
" show_label=False,\n",
" container=False,\n",
" )\n",
" with gr.Column():\n",
" with gr.Row():\n",
" submit = gr.Button(\"Submit\", variant=\"primary\")\n",
" stop = gr.Button(\"Stop\")\n",
" clear = gr.Button(\"Clear\")\n",
" with gr.Column(scale=1):\n",
" agent_interactive = self.agent_list[0]\n",
" capabilities = [key for key in agent_interactive.function_map.keys()]\n",
" gr.CheckboxGroup(\n",
" label=\"Tools\",\n",
" value=capabilities,\n",
" choices=capabilities,\n",
" interactive=False,\n",
" )\n",
" with gr.Row():\n",
" gr.Examples(self.prompt_suggestions, inputs=[input], label=\"Click on any example and press the 'Submit' button\")\n",
"\n",
" input_promise = submit.click(\n",
" fn=self.add_text,\n",
" inputs=[input, chatbot, history],\n",
" outputs=[input, chatbot, history],\n",
" queue=False,\n",
" )\n",
" input_promise = input_promise.then(\n",
" self.agent_run,\n",
" [chatbot, history],\n",
" [chatbot, history],\n",
" )\n",
" input_promise.then(self.flushed, None, [input])\n",
" stop.click(\n",
" fn=self.request_cancel,\n",
" inputs=None,\n",
" outputs=None,\n",
" cancels=[input_promise],\n",
" queue=False,\n",
" )\n",
" clear.click(lambda: None, None, chatbot, queue=False).then(self.clear_history, None, history)\n",
"\n",
" self.demo.load(None)\n",
"\n",
" self.demo.launch(share=share, server_name=server_name, server_port=server_port)\n",
"\n",
"\n",
"chatbot_config = {\n",
" \"prompt.suggestions\": [\n",
" \"Based on current weather in London, show me a picture of Big Ben\",\n",
" \"What is OpenVINO ?\",\n",
" \"Create an image of pink cat\",\n",
" \"What is the weather like in New York now ?\",\n",
" \"How many people live in Canada ?\",\n",
" ],\n",
" \"agent.avatar\": openvino_logo,\n",
" \"input.placeholder\": \"Please input your request here\",\n",
"}\n",
"if not Path(\"gradio_helper.py\").exists():\n",
" r = requests.get(url=\"https://raw.githubusercontent.com/openvinotoolkit/openvino_notebooks/latest/notebooks/llm-agent-functioncall/gradio_helper.py\")\n",
" open(\"gradio_helper.py\", \"w\").write(r.text)\n",
"\n",
"demo = OpenVINOUI(\n",
" bot,\n",
" chatbot_config=chatbot_config,\n",
")\n",
"from gradio_helper import make_demo\n",
"\n",
"demo = make_demo(bot=bot)\n",
"\n",
"# if you are launching remotely, specify server_name and server_port\n",
"# demo.run(server_name='your server name', server_port='server port in int')\n",
"try:\n",
" demo.run()\n",
"except Exception:\n",
" demo.run(share=True)"
" demo.run(share=True)\n",
"# If you are launching remotely, specify server_name and server_port\n",
"# EXAMPLE: `demo.launch(server_name='your server name', server_port='server port in int')`\n",
"# To learn more please refer to the Gradio docs: https://gradio.app/docs/"
]
},
{
Expand All @@ -736,7 +607,8 @@
"metadata": {},
"outputs": [],
"source": [
"# demo.demo.close()"
"# please uncomment and run this cell for stopping gradio interface\n",
"# demo.close()"
]
}
],
Expand Down
Loading

0 comments on commit fd22e96

Please sign in to comment.