-
Notifications
You must be signed in to change notification settings - Fork 17
/
multimodal_chat.py
1155 lines (999 loc) · 40.5 KB
/
multimodal_chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import base64
import io
import os
import sys
import uuid
from datetime import datetime
from functools import partial
from inspect import getframeinfo, stack
from typing import Any, Dict, List, Tuple
import filetype
import nest_asyncio
import numpy as np
import pandas as pd
import requests
import streamlit as st
import torch
import yfinance
from audiocraft.data.audio import audio_write
from audiocraft.models import musicgen
from audiocraft.utils.notebook import display_audio
from langchain import hub
from langchain.agents import Tool, load_tools
from langchain.agents.agent import AgentExecutor
from langchain.agents.structured_chat.base import create_structured_chat_agent
from langchain.memory import ChatMessageHistory
from langchain.prompts import HumanMessagePromptTemplate, MessagesPlaceholder
from langchain.schema.output_parser import StrOutputParser
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.tools import tool
from langchain_anthropic import ChatAnthropic
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.tools.wikidata.tool import WikidataAPIWrapper, WikidataQueryRun
from langchain_community.tools.wikipedia.tool import WikipediaQueryRun
from langchain_community.utilities import GoogleSearchAPIWrapper
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
from langchain_community.vectorstores import FAISS
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import BaseMessage, HumanMessage, SystemMessage
from langchain_core.messages.tool import ToolMessage, ToolMessageChunk
from langchain_core.prompts import PromptTemplate
from langchain_core.prompts.chat import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.runnables import Runnable, RunnablePassthrough
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_core.tools import Tool
from langchain_experimental.llms.anthropic_functions import AnthropicFunctions
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from matplotlib import pyplot as plt
from mutagen.mp3 import MP3
from openai import OpenAI
from PIL import Image, UnidentifiedImageError
from rich.pretty import pprint
from transformers import pipeline
from ultralytics import YOLO
from ultralytics.engine.results import Boxes, Results
VERBOSE = True
MAX_TOKEN = 2048
LOOK_UP_LLM = {
"claude-3-5-sonnet-20240620": {
"model": ChatAnthropic(
model="claude-3-5-sonnet-20240620",
temperature=st.session_state.get("key_temperature", 0.0),
max_tokens=MAX_TOKEN,
),
"streaming": False, # Anthropic has content of a list of dict, the fist element is the key "text", value is the text, streaming wont work atm for streamlit.
},
"gpt-4o": {
"model": ChatOpenAI(
model="gpt-4o",
temperature=st.session_state.get("key_temperature", 0.0),
max_tokens=MAX_TOKEN,
),
"streaming": True,
},
"gemini-1.5-flash-latest": {
"model": ChatGoogleGenerativeAI(
model="gemini-1.5-flash-latest",
temperature=st.session_state.get("key_temperature", 0.0),
max_tokens=MAX_TOKEN,
),
"streaming": True,
},
}
CV_MODEL: str = "yolov8s-world.pt"
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
FUN_MAPPING = {}
nest_asyncio.apply()
st.set_page_config(layout="wide")
# ------------------------------------------helpers------------------------------------------
def pretty_print(title: str = "Untitled", content: Any = None):
if not VERBOSE:
return
info = getframeinfo(stack()[1][0])
print()
pprint(
f":--> {title} --> {info.filename} --> {info.function} --> line: {info.lineno} --:"
)
pprint(content)
def image_url_to_image(image_url: str) -> Image:
return Image.open(requests.get(image_url, stream=True).raw)
def create_random_filename(ext=".txt") -> str:
return create_random_name() + ext
def create_random_name() -> str:
return str(uuid.uuid4())
# enum class for audio or image
class MediaType:
AUDIO = "audio"
IMAGE = "image"
def doc_uploader() -> Tuple[bytes, str]:
with st.sidebar:
uploaded_doc = st.file_uploader(
"# Upload one image or an audio file", key="doc_uploader"
)
if not uploaded_doc:
st.session_state["file_name"] = None
st.session_state["base64_object"] = None
# pretty_print("doc_uploader", "No image uploaded")
return None
if uploaded_doc:
tmp_dir = "./tmp"
if not os.path.exists(tmp_dir):
os.makedirs(tmp_dir)
temp_file_path = os.path.join(tmp_dir, f"{uploaded_doc.name}")
with open(temp_file_path, "wb") as file:
file.write(uploaded_doc.getvalue())
file_name = uploaded_doc.name
media_type = (
MediaType.IMAGE
if filetype.is_image(temp_file_path)
else MediaType.AUDIO
)
# pretty_print("doc_uploader", f"Uploaded {file_name}")
uploaded_doc.flush()
uploaded_doc.close()
# os.remove(temp_file_path)
if st.session_state.get("file_name") == file_name:
# pretty_print("doc_uploader", "Same file")
return st.session_state["base64_object"]
# pretty_print("doc_uploader", "New file")
st.session_state["file_name"] = temp_file_path
with open(temp_file_path, "rb") as image_file:
st.session_state["base64_object"] = base64.b64encode(
image_file.read()
).decode("utf-8")
return st.session_state["base64_object"], media_type
return None
# ------------------------------------------LLM Chain------------------------------------------
def create_message_chain(
model: BaseChatModel,
base64_object: bytes,
media_type: str,
) -> Runnable:
handle_image = media_type == MediaType.IMAGE
handle_audio = media_type == MediaType.AUDIO
pretty_print("handle_image", handle_image)
pretty_print("handle_audio", handle_audio)
prompt = ChatPromptTemplate.from_messages(
[
SystemMessage(
content="""As a helpful assistant, you should respond to the user's query.
Avoid giving any information related to the local file system or sandbox."""
),
MessagesPlaceholder(variable_name="history"),
HumanMessagePromptTemplate.from_template(
template=(
[
{
"type": "text",
"text": "{query}",
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_object}",
},
},
]
if handle_image
else [
{"type": "text", "text": "{query}"},
]
)
),
]
)
return prompt | model
def create_tool_chain(model: BaseChatModel) -> Runnable:
return model
# ------------------------------------------agent, functions, tools------------------------------------------
class LoadUrlsTool(BaseModel):
query: str = Field(description="The query to search for.")
urls: List[str] = Field(description="The URLs to load.")
@tool("load-urls-tool", args_schema=LoadUrlsTool, return_direct=False)
def load_urls_tool(retrieval_model: BaseChatModel, query: str, urls: List[str]) -> str:
"""Load the content of the given Urls for getting responses to the query and return the query result."""
load_urls_prompt_template = PromptTemplate.from_template(
"""Reponse the query inside [query] based on the context inside [context]:
[query]
{query}
[query]
[context]
{context}
[context]
Only return the answer without any instruction text or additional information.
Keep the result as simple as possible."""
)
loader = WebBaseLoader(urls)
docs = loader.load()
splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
chunk_size=1000, chunk_overlap=0
)
chunks = splitter.split_documents(docs)
db = FAISS.from_documents(chunks, OpenAIEmbeddings())
retriever = db.as_retriever()
chain = (
{"context": retriever, "query": RunnablePassthrough()}
| load_urls_prompt_template
| retrieval_model
| StrOutputParser()
)
return chain.invoke(query)
search_agent_tools = [
Tool(
name="Google Search",
description="Search Google for recent results.",
func=GoogleSearchAPIWrapper(
google_api_key=os.environ.get("GOOGLE_CSE_KEY")
).run,
),
Tool(
name="DuckDuckGo Search",
func=DuckDuckGoSearchRun().run,
description="Use to search for the information from DuckDuckGo.",
),
Tool(
name="Wikipedia Search",
func=WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()).run,
description="Use to search for the information from Wikipedia.",
),
Tool(
name="Wikidata Search",
func=WikidataQueryRun(api_wrapper=WikidataAPIWrapper()).run,
description="Use to search for the information from Wikidata.",
),
]
search_agent_tools.extend(load_tools(["arxiv"]))
def create_search_agent(agent_model: BaseChatModel) -> AgentExecutor:
return AgentExecutor(
agent=create_structured_chat_agent(
llm=agent_model,
tools=search_agent_tools,
prompt=hub.pull("hwchase17/structured-chat-agent"),
),
tools=search_agent_tools,
handle_parsing_errors=True,
return_intermediate_steps=True,
)
# ------------------------------------------tool functions------------------------------------------
def generate_image(model: BaseChatModel, context: str) -> str:
"""Generate an image to illustrate for user request."""
tools = load_tools(["dalle-image-generator"])
agent = create_structured_chat_agent(
llm=model,
tools=tools,
prompt=hub.pull("hwchase17/structured-chat-agent"),
)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
handle_parsing_errors=True,
return_intermediate_steps=True,
)
prompt = f"""Generate an image to illustrate for user request with the following context:
Context:
{context}
Notice:
- ONLY return the image link.
- WHEN the image includes text, it MUST be in the same language as the language of the input text.
"""
image_gen = agent_executor.invoke({"input": prompt})
# pretty_print("Image Gen:", image_gen)
try:
image_url = image_gen["output"]
return image_url_to_image(image_url), image_url
except UnidentifiedImageError:
return None, None
def annotate_image(
model: BaseChatModel,
base64_image: bytes,
image_description: str,
) -> Tuple[Results, str]:
"""Annotate an image with classes (COCO dataset types), response results with bounding boxes."""
def coco_label_extractor(model: BaseChatModel, image_description: str) -> str:
"""Read an image description and extract COCO defined labels as much as possible from the description."""
template = ChatPromptTemplate.from_messages(
[
(
"system",
"""You as an AI assistant can understand an image descritpion.
Try to extract COCO defined labels as much as possible from the description.
Only return lables and split by comma, no empty space.""",
),
("human", "Image descritpion: {img_desc}"),
]
)
human_input = template.format_messages(img_desc=image_description)
return model.invoke(human_input).content
# pretty_print("Image description:", image_description)
classes = coco_label_extractor(model, image_description)
# pretty_print("Classes:", classes)
classes = classes.split(",") if classes else list()
model = YOLO(CV_MODEL) # or select yolov8m/l-world.pt for different sizes
if classes is not None and len(classes) > 0:
model.set_classes(classes)
image = Image.open(io.BytesIO(base64.b64decode(base64_image)))
preds = model.predict(image)
results: Results = preds[0]
save_dir = "tmp"
if not os.path.exists(save_dir):
os.makedirs(save_dir)
save_path = f"{save_dir}/annotated_{create_random_filename('.jpg')}"
results.save(save_path)
return results, save_path
def run_search_agent(agent: AgentExecutor, topic: str) -> Dict[str, Any]:
"""Run the agent for the topic, the agent will search the topic through the web and return the result."""
prompt = f"""Search through the internet for the topic inside >>>>>>>>> and <<<<<<<<<<
>>>>>>>>>
{topic}
<<<<<<<<<<
We recommend certain tools that you can use:
- Google Search
- DuckDuckGo Search
- Wikipedia Search
- Wikidata Search
- arxiv Search
Also if you face some urls for more details you can also use the tool:
- load-urls-tool
"""
return agent.invoke({"input": prompt})
def get_current_time(_: str) -> str:
"""For questions or queries that are relevant to current time or date"""
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return current_time
def get_and_plot_stock_prices(
stock_symbols: List[str], start_date: str, end_date: str
) -> Tuple[pd.DataFrame, str]:
"""Get and plot the stock prices for the given stock symbols between
the start and end dates.
Args:
stock_symbols (str or list): The stock symbols to get the
prices for.
start_date (str): The start date in the format
'YYYY-MM-DD'.
end_date (str): The end date in the format 'YYYY-MM-DD'.
Returns:
Tuple:
pandas.DataFrame: The stock prices for the given stock symbols indexed by date, with one column per stock symbol.
stock_prices (pandas.DataFrame): The stock prices for the
given stock symbols.
"""
def _plot_stock_prices(stock_prices: pd.DataFrame, filename: str):
"""Plot the stock prices for the given stock symbols.
Args:
stock_prices (pandas.DataFrame): The stock prices for the given stock symbols.
filename (str): The filename to save the plot to.
"""
plt.figure(figsize=(10, 5))
for column in stock_prices.columns:
plt.plot(stock_prices.index, stock_prices[column], label=column)
plt.title("Stock Prices")
plt.xlabel("Date")
plt.ylabel("Price")
plt.legend(stock_prices)
plt.grid(True)
plt.savefig(filename)
stock_data = yfinance.download(stock_symbols, start=start_date, end=end_date)
pretty_print("stock_data:", stock_data)
prices_df = stock_data.get("Close")
pretty_print("Close:", prices_df)
plot_filename = create_random_filename(ext=".png")
fullpath = f"./tmp/{plot_filename}"
_plot_stock_prices(prices_df, fullpath)
return (prices_df, fullpath)
def text2speech(text: str, voice_type: str = "alloy") -> str:
"""Convert text to speech, return the full filepath to the speech."""
client = OpenAI()
random_filename = create_random_filename(".mp3")
speech_file_path = f"./tmp/{random_filename}"
response = client.audio.speech.create(
model="tts-1",
voice=voice_type,
input=text,
)
response.stream_to_file(speech_file_path)
return speech_file_path
def text2music(prompt: str, duration=15) -> str:
"""Convert prompting text to music, return the full filepath to the music."""
def _write_wav(output, file_initials):
try:
for idx, one_wav in enumerate(output):
audio_write(
f"{file_initials}_{idx}",
one_wav.cpu(),
model.sample_rate,
strategy="loudness",
loudness_compressor=True,
)
return True
except Exception as e:
print("Error while writing the file ", e)
return None
model = musicgen.MusicGen.get_pretrained("medium", device=DEVICE)
model.set_generation_params(duration=15)
musicgen_out = model.generate([prompt], progress=True)
musicgen_out_filename = f"./tmp/{create_random_name()}"
_write_wav(musicgen_out, musicgen_out_filename)
return f"{musicgen_out_filename}_0.wav"
def speech2text(base64_speech_audio: bytes) -> str:
"""Convert base64_speech_audio to text, extract the base64_speech_audio content, and provide the text as return."""
audio_file_name = "./tmp/temp.wav"
audio_file = open(audio_file_name, "wb")
bytes_io = io.BytesIO(base64.b64decode(base64_speech_audio))
audio_file.write(bytes_io.read())
torch_dtype = torch.float32 if DEVICE == "cpu" else torch.float16
pipe_aud2txt = pipeline(
"automatic-speech-recognition",
"openai/whisper-large-v3",
torch_dtype=torch_dtype,
device=DEVICE,
)
res = pipe_aud2txt(audio_file_name)
return res.get("text")
def synthesize_audio(
speech_text: str, prompt: str, voice_type: str = "alloy", duration=15
) -> str:
"""Generate an audio using the provided speech_text and background music audio generated from the prompt.
Generate an audio from the speech_text and another background music from the prompt, then combine the two audioes
into a single synthesis.
"""
text2speech_file_fullpath = text2speech(speech_text, voice_type)
text2music_file_fullpath = text2music(prompt, duration)
synthesis_filename = f"./tmp/{create_random_filename('.mp3')}"
os.system(
f"ffmpeg -i {text2speech_file_fullpath} -stream_loop -1 -i {text2music_file_fullpath} -filter_complex amix=inputs=2:duration=first:dropout_transition=2 {synthesis_filename} -y"
)
return synthesis_filename
# ------------------------------------------tool------------------------------------------
class GenerateImageTool(BaseModel):
"""Generate an image to illustrate for user request."""
context: str = Field(
...,
description="The context for generating an image to illustrate what the user requested.",
)
class AnnotateImageTool(BaseModel):
"""Annotate an image with classes (COCO dataset types), response results with bounding boxes."""
image_description: str = Field(
...,
description="The description of the image that needs to be annotated.",
)
class RunSearchAgentTool(BaseModel):
"""Run the agent to search for corresponding topic. The agent will search the web for the topic and return the results."""
topic: str = Field(
...,
description="The topic to search through the web.",
)
class GetCurrentTimeTool(BaseModel):
"""Get the current time."""
event: str = Field(
...,
description="Some questions or queries that are relevant to current time or date",
)
class GetAndPlotStockPricesTool(BaseModel):
"""Get and plot the stock prices for the given stock symbols between the start and end dates."""
stock_symbols: str = Field(
...,
description="The stock symbols list string to get prices for, should be a string with comma-separated different symbols.",
)
start_date: str = Field(
...,
description="The start date in the format 'YYYY-MM-DD' or other understandable format.",
)
end_date: str = Field(
...,
description="The end date in the format 'YYYY-MM-DD' or other understandable format.",
)
class Text2SpeechTool(BaseModel):
"""Convert text to speech."""
text: str = Field(
...,
description="The text that will be converted to speech.",
)
class Text2MusicTool(BaseModel):
"""Tool for generating music via text prompting."""
prompt: str = Field(
...,
description="Use the prompt text to generate music.",
)
class Speech2TextTool(BaseModel):
"""Extract speech audio content to text."""
pass
class SynthesizeAudioTool(BaseModel):
"""Synthesize audio based on the provided speech_text and prompt."""
speech_text: str = Field(
...,
description="The text that will be converted to speech.",
)
prompt: str = Field(
None,
description="The prompt that will be used to generate background music audio, if not set, the prompt will be the same as the speech_text.",
)
# ------------------------------------------model event handlers------------------------------------------
def handle_generate_image(
tool_name: str,
tool_id: str,
context: str,
) -> ToolMessage:
additional_kwargs = dict()
try:
func = FUN_MAPPING.get(tool_name, None)
if func:
image, image_url = func(context=context)
else:
raise ValueError("No tool provided.")
additional_kwargs = {"image_url": image_url} if image_url else {}
return ToolMessage(
content=(
f"Generated image successfully, tool has finished."#, tell the users to use the following link to view the image: ![]({image_url})"
if image_url
else "Tool called, nothing was generated"
),
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
except Exception as e:
st.write(f"Something went wrong.\n\n{e}")
return ToolMessage(
content=f"Tool was called but failed to generate image.\n\n{e}",
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
def handle_annotate_image(
tool_name: str,
tool_id: str,
base64_image: bytes,
image_description: str,
) -> ToolMessage:
additional_kwargs = {}
try:
func = FUN_MAPPING.get(tool_name, None)
if func:
_, image_path = func(
base64_image=base64_image, image_description=image_description
)
else:
raise ValueError("No tool provided.")
additional_kwargs["image_path"] = image_path if image_path else ""
return ToolMessage(
content=(
f"Annotated image successfully, tool has finished."
if image_path
else "Tool called, nothing was annotated"
),
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
except Exception as e:
st.write(f"Something went wrong.\n\n{e}")
return ToolMessage(
content=f"Tool was called but failed to annotate image.\n\n{e}",
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
def handle_search_agent(
tool_name: str,
tool_id: str,
topic: str,
) -> ToolMessage:
additional_kwargs = {}
try:
func = FUN_MAPPING.get(tool_name, None)
if func:
agent_res = func(topic)
else:
raise ValueError("No tool provided.")
additional_kwargs["string"] = agent_res["output"]
return ToolMessage(
content=(
f"Searched successfully, tool has finished:\n\n{agent_res['output']}"
if agent_res["output"] and agent_res["output"] != ""
else "Tool called, nothing was responsed by agent."
),
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
except Exception as e:
st.write(f"Something went wrong.\n\n{e}")
return ToolMessage(
content=f"Tool was called but failed to get result.\n\n{e}",
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
def handle_get_current_time(
tool_name: str,
tool_id: str,
) -> ToolMessage:
additional_kwargs = {}
try:
func = FUN_MAPPING.get(tool_name, None)
if func:
current_time = func("")
else:
raise ValueError("No tool provided.")
additional_kwargs["string"] = f"Current time: {current_time}"
return ToolMessage(
content=(
f"Get current time: {current_time}, tool has finished."
if current_time and current_time != ""
else "Tool called, nothing was responsed by agent."
),
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
except Exception as e:
st.write(f"Something went wrong.\n\n{e}")
return ToolMessage(
content=f"Tool was called but failed to get current time.\n\n{e}",
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
def handle_get_stock_prices(
tool_name: str,
tool_id: str,
tool_stock_symbols: str,
tool_start_date: str,
tool_end_date: str,
) -> ToolMessage:
additional_kwargs = {}
try:
func = FUN_MAPPING.get(tool_name, None)
if func:
prices_df, image_path = func(
stock_symbols=tool_stock_symbols.split(","),
start_date=tool_start_date,
end_date=tool_end_date,
)
else:
raise ValueError("No tool provided.")
additional_kwargs["image_path"], additional_kwargs["dataframe"] = (
image_path,
prices_df.to_string(),
)
return ToolMessage(
content=f"""Complete getting stock prices successfully, tool has finished, stop further investigation. The stock prices requested:
dataframe:
{prices_df.to_string()}""",
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
except Exception as e:
st.write(f"Something went wrong.\n\n{e}")
return ToolMessage(
content=f"Tool was called but failed to get stock prices completely: {e}",
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
def handle_text2audio(
tool_name: str,
tool_id: str,
**kvargs,
) -> ToolMessage:
additional_kwargs = {}
try:
func = FUN_MAPPING.get(tool_name, None)
if func:
audio_path = func(**kvargs)
else:
raise ValueError("No tool provided.")
additional_kwargs["audio_path"] = audio_path
return ToolMessage(
content=(
f"Audio has been generated successfully, tool has finished, an audio path: {audio_path}"
if audio_path and audio_path != ""
else "Tool called, nothing was responsed by model."
),
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
except Exception as e:
st.write(f"Something went wrong.\n\n{e}")
return ToolMessage(
content=f"Tool was called but failed for:\n\n{e}",
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
def handle_speech2text(
tool_name: str,
tool_id: str,
base64_speech_audio: bytes,
) -> ToolMessage:
additional_kwargs = {}
try:
func = FUN_MAPPING.get(tool_name, None)
if func:
text = func(base64_speech_audio)
else:
raise ValueError("No tool provided.")
additional_kwargs["string"] = text
return ToolMessage(
content=(
f"Speech to Text successfully, tool has finished, output text:\n\n{text}"
if text and text != ""
else "Tool called, nothing was responsed by model."
),
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
except Exception as e:
st.write(f"Something went wrong.\n\n{e}")
return ToolMessage(
content=f"Tool was called but failed for:\n\n{e}",
tool_call_id=tool_id,
additional_kwargs=additional_kwargs,
)
# ------------------------------------------chat-----------------------------------------------
def tool_call_proc(
tool_call: Dict,
base64_object: bytes = None,
) -> ToolMessage:
tool_id, tool_name = tool_call["id"], tool_call["name"]
match tool_name:
case "GenerateImageTool":
context = tool_call["args"]["context"]
return handle_generate_image(tool_name, tool_id, context)
case "AnnotateImageTool":
image_description = tool_call["args"]["image_description"]
return handle_annotate_image(
tool_name, tool_id, base64_object, image_description
)
case "RunSearchAgentTool":
topic = tool_call["args"]["topic"]
return handle_search_agent(tool_name, tool_id, topic)
case "Text2SpeechTool":
text = tool_call["args"]["text"]
return handle_text2audio(tool_name, tool_id, text=text)
case "Text2MusicTool":
prompt = tool_call["args"]["prompt"]
return handle_text2audio(tool_name, tool_id, prompt=prompt)
case "SynthesizeAudioTool":
speech_text = tool_call["args"]["speech_text"]
prompt = tool_call["args"].get("prompt", speech_text)
return handle_text2audio(
tool_name, tool_id, speech_text=speech_text, prompt=prompt
)
case "Speech2TextTool":
return handle_speech2text(tool_name, tool_id, base64_object)
case "GetCurrentTimeTool":
return handle_get_current_time(tool_name, tool_id)
case "GetAndPlotStockPricesTool":
now = datetime.now()
tool_stock_symbols = tool_call["args"]["stock_symbols"]
tool_start_date = tool_call["args"].get("start_date", now)
tool_end_date = tool_call["args"].get("end_date", now)
return handle_get_stock_prices(
tool_name, tool_id, tool_stock_symbols, tool_start_date, tool_end_date
)
case _:
return ToolMessage(
content=f"Handle the UNKNOWN tool call: {tool_name}",
tool_call_id=tool_id,
additional_kwargs=dict(),
)
def chat_with_model(
model: BaseChatModel,
base64_object: bytes = None,
media_type: str = None,
streaming=False,
image_width=500,
audio_auto_play=True,
):
if "messages" not in st.session_state:
st.session_state.messages = []
if "history" not in st.session_state:
st.session_state.history = ChatMessageHistory()
for message in st.session_state.messages:
# pretty_print("message", message)
if message["role"] == "tool":
if message["additional_kwargs"].get("image_path"):
with st.chat_message("assistant"):
st.image(
message["additional_kwargs"]["image_path"], width=image_width
)
if message["additional_kwargs"].get("image_url"):
with st.chat_message("assistant"):
st.image(
message["additional_kwargs"]["image_url"], width=image_width
)
if message["additional_kwargs"].get("audio_path"):
with st.chat_message("assistant"):
st.audio(
message["additional_kwargs"]["audio_path"],
autoplay=audio_auto_play,
)
else:
with st.chat_message(message["role"]):
# pretty_print("message[\"content\"]", message["content"])
st.markdown(message["content"])
if prompt := st.chat_input("Write...", key="chat_input"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
should_continue, tool_messages = True, None
while should_continue:
if not tool_messages:
chat_chain = RunnableWithMessageHistory(
create_message_chain(
model,
base64_object,
media_type,
),
lambda _: st.session_state.history,
input_messages_key="query",
history_messages_key="history",
)
call_model = (
chat_chain.invoke if not streaming else chat_chain.stream
)
res = call_model(
{"query": prompt},
{"configurable": {"session_id": None}},
)
else:
chat_chain = RunnableWithMessageHistory(
create_tool_chain(model), lambda _: st.session_state.history
)
call_model = (
chat_chain.invoke if not streaming else chat_chain.stream
)
res = call_model(
{None: tool_messages},
{"configurable": {"session_id": None}},
)
del tool_messages
content, additional_kwargs, tool_calls = None, dict(), None
if not streaming:
content = res.content
content = (
content
if not isinstance(content, list)
else content[0]["text"]
) # Anthropic has content of a list of dict, the fist element is the key "text", value is the text.
st.markdown(content)
else: