Skip to content

Commit

Permalink
add image input
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketmaurya committed May 15, 2024
1 parent 37bde72 commit 52d6ac3
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions examples/chatbot/app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import gradio as gr
from agents.llms.llm import LlamaCppChatCompletion
from agents.tool_executor import need_tool_use
from agents.tools import get_current_weather, wikipedia_search, google_search
from agents.tools import (
get_current_weather,
wikipedia_search,
google_search,
image_inspector,
)

llm = LlamaCppChatCompletion.from_default_llm(n_ctx=0)
llm.bind_tools([get_current_weather, google_search, wikipedia_search])
llm.bind_tools([get_current_weather, google_search, wikipedia_search, image_inspector])


def llamacpp_chat(message, history):
def llamacpp_chat(message, history, image=None):
history_langchain_format = [
{
"role": "system",
Expand All @@ -17,7 +22,12 @@ def llamacpp_chat(message, history):
for user, ai in history:
history_langchain_format.append({"role": "user", "content": user})
history_langchain_format.append({"role": "assistant", "content": ai})
history_langchain_format.append({"role": "user", "content": message})

if image:
message += f"\nAttached image filepath: ```{image}```"
history_langchain_format.append({"role": "user", "content": message})
else:
history_langchain_format.append({"role": "user", "content": message})
messages = history_langchain_format

output = llm.chat_completion(messages)
Expand All @@ -34,6 +44,7 @@ def llamacpp_chat(message, history):

gr.ChatInterface(
llamacpp_chat,
additional_inputs=[gr.Image(type="filepath")],
chatbot=gr.Chatbot(height=300, likeable=True),
textbox=gr.Textbox(placeholder="Ask me any question", container=False, scale=7),
title="Agent",
Expand All @@ -43,4 +54,4 @@ def llamacpp_chat(message, history):
retry_btn=None,
undo_btn="Delete Previous",
clear_btn="Clear",
).launch()
).queue().launch(server_name="0.0.0.0")

0 comments on commit 52d6ac3

Please sign in to comment.