Spaces:
Paused
Paused
NGUYEN, Xuan Phi
commited on
Commit
·
7194bc8
1
Parent(s):
3a2a429
update
Browse files
multipurpose_chatbot/demos/langchain_web_search.py
CHANGED
|
@@ -144,6 +144,7 @@ class AnyEnginePipeline(BaseLLM):
|
|
| 144 |
# List to hold all results
|
| 145 |
text_generations: List[str] = []
|
| 146 |
stop_strings = stop
|
|
|
|
| 147 |
for i in range(0, len(prompts), self.batch_size):
|
| 148 |
batch_prompts = prompts[i : i + self.batch_size]
|
| 149 |
responses = []
|
|
@@ -156,7 +157,6 @@ class AnyEnginePipeline(BaseLLM):
|
|
| 156 |
text = text[len(prompt):]
|
| 157 |
if stop is not None and any(x in text for x in stop):
|
| 158 |
text = text[:text.index(stop[0])]
|
| 159 |
-
# print(f">>{text}")
|
| 160 |
text_generations.append(text)
|
| 161 |
return LLMResult(
|
| 162 |
generations=[[Generation(text=text)] for text in text_generations]
|
|
@@ -456,8 +456,7 @@ Let's begin! Below is the question from the user.
|
|
| 456 |
def create_web_search_engine(model_engine=None):
|
| 457 |
# from langchain_community.tools.tavily_search import TavilySearchResults
|
| 458 |
if model_engine is None:
|
| 459 |
-
|
| 460 |
-
model_engine = MODEL_ENGINE
|
| 461 |
|
| 462 |
from langchain_core.utils.function_calling import (
|
| 463 |
convert_to_openai_function,
|
|
@@ -472,8 +471,6 @@ def create_web_search_engine(model_engine=None):
|
|
| 472 |
|
| 473 |
tools = [NewTavilySearchResults(max_results=1)]
|
| 474 |
formatted_tools = [convert_to_openai_tool(tool) for tool in tools]
|
| 475 |
-
# tools = load_tools(["llm-math"], llm=web_search_llm)
|
| 476 |
-
# formatted_tools = render_text_description_and_args(tools)
|
| 477 |
prompt_template = ChatPromptTemplate.from_messages(
|
| 478 |
[
|
| 479 |
# (
|
|
@@ -510,249 +507,3 @@ def create_web_search_engine(model_engine=None):
|
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
# if LANGCHAIN_AVAILABLE:
|
| 516 |
-
# class LooseReActJsonSingleInputOutputParser(ReActJsonSingleInputOutputParser):
|
| 517 |
-
# def parse(self, text: str) -> AgentAction | AgentFinish:
|
| 518 |
-
# try:
|
| 519 |
-
# return super().parse(text)
|
| 520 |
-
# except OutputParserException as e:
|
| 521 |
-
# return AgentFinish({"output": text}, text)
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
# class ChatHuggingfaceFromLocalPipeline(ChatHuggingFace):
|
| 525 |
-
# @root_validator()
|
| 526 |
-
# def validate_llm(cls, values: dict) -> dict:
|
| 527 |
-
# return values
|
| 528 |
-
# def _resolve_model_id(self) -> None:
|
| 529 |
-
# """Resolve the model_id from the LLM's inference_server_url"""
|
| 530 |
-
# self.model_id = self.llm.model_id
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
# class NewHuggingfacePipeline(HuggingFacePipeline):
|
| 534 |
-
# bos_token = "<bos>"
|
| 535 |
-
# add_bos_token = True
|
| 536 |
-
|
| 537 |
-
# @classmethod
|
| 538 |
-
# def from_model_id(
|
| 539 |
-
# cls,
|
| 540 |
-
# model_id: str,
|
| 541 |
-
# task: str,
|
| 542 |
-
# backend: str = "default",
|
| 543 |
-
# device: Optional[int] = -1,
|
| 544 |
-
# device_map: Optional[str] = None,
|
| 545 |
-
# model_kwargs: Optional[dict] = None,
|
| 546 |
-
# pipeline_kwargs: Optional[dict] = None,
|
| 547 |
-
# batch_size: int = 2,
|
| 548 |
-
# model = None,
|
| 549 |
-
# **kwargs: Any,
|
| 550 |
-
# ) -> HuggingFacePipeline:
|
| 551 |
-
# """Construct the pipeline object from model_id and task."""
|
| 552 |
-
# try:
|
| 553 |
-
# from transformers import (
|
| 554 |
-
# AutoModelForCausalLM,
|
| 555 |
-
# AutoModelForSeq2SeqLM,
|
| 556 |
-
# AutoTokenizer,
|
| 557 |
-
# )
|
| 558 |
-
# from transformers import pipeline as hf_pipeline
|
| 559 |
-
|
| 560 |
-
# except ImportError:
|
| 561 |
-
# raise ValueError(
|
| 562 |
-
# "Could not import transformers python package. "
|
| 563 |
-
# "Please install it with `pip install transformers`."
|
| 564 |
-
# )
|
| 565 |
-
|
| 566 |
-
# _model_kwargs = model_kwargs or {}
|
| 567 |
-
# tokenizer = AutoTokenizer.from_pretrained(model_id, **_model_kwargs)
|
| 568 |
-
# if model is None:
|
| 569 |
-
# try:
|
| 570 |
-
# if task == "text-generation":
|
| 571 |
-
# if backend == "openvino":
|
| 572 |
-
# try:
|
| 573 |
-
# from optimum.intel.openvino import OVModelForCausalLM
|
| 574 |
-
|
| 575 |
-
# except ImportError:
|
| 576 |
-
# raise ValueError(
|
| 577 |
-
# "Could not import optimum-intel python package. "
|
| 578 |
-
# "Please install it with: "
|
| 579 |
-
# "pip install 'optimum[openvino,nncf]' "
|
| 580 |
-
# )
|
| 581 |
-
# try:
|
| 582 |
-
# # use local model
|
| 583 |
-
# model = OVModelForCausalLM.from_pretrained(
|
| 584 |
-
# model_id, **_model_kwargs
|
| 585 |
-
# )
|
| 586 |
-
|
| 587 |
-
# except Exception:
|
| 588 |
-
# # use remote model
|
| 589 |
-
# model = OVModelForCausalLM.from_pretrained(
|
| 590 |
-
# model_id, export=True, **_model_kwargs
|
| 591 |
-
# )
|
| 592 |
-
# else:
|
| 593 |
-
# model = AutoModelForCausalLM.from_pretrained(
|
| 594 |
-
# model_id, **_model_kwargs
|
| 595 |
-
# )
|
| 596 |
-
# elif task in ("text2text-generation", "summarization", "translation"):
|
| 597 |
-
# if backend == "openvino":
|
| 598 |
-
# try:
|
| 599 |
-
# from optimum.intel.openvino import OVModelForSeq2SeqLM
|
| 600 |
-
|
| 601 |
-
# except ImportError:
|
| 602 |
-
# raise ValueError(
|
| 603 |
-
# "Could not import optimum-intel python package. "
|
| 604 |
-
# "Please install it with: "
|
| 605 |
-
# "pip install 'optimum[openvino,nncf]' "
|
| 606 |
-
# )
|
| 607 |
-
# try:
|
| 608 |
-
# # use local model
|
| 609 |
-
# model = OVModelForSeq2SeqLM.from_pretrained(
|
| 610 |
-
# model_id, **_model_kwargs
|
| 611 |
-
# )
|
| 612 |
-
|
| 613 |
-
# except Exception:
|
| 614 |
-
# # use remote model
|
| 615 |
-
# model = OVModelForSeq2SeqLM.from_pretrained(
|
| 616 |
-
# model_id, export=True, **_model_kwargs
|
| 617 |
-
# )
|
| 618 |
-
# else:
|
| 619 |
-
# model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 620 |
-
# model_id, **_model_kwargs
|
| 621 |
-
# )
|
| 622 |
-
# else:
|
| 623 |
-
# raise ValueError(
|
| 624 |
-
# f"Got invalid task {task}, "
|
| 625 |
-
# f"currently only {VALID_TASKS} are supported"
|
| 626 |
-
# )
|
| 627 |
-
# except ImportError as e:
|
| 628 |
-
# raise ValueError(
|
| 629 |
-
# f"Could not load the {task} model due to missing dependencies."
|
| 630 |
-
# ) from e
|
| 631 |
-
# else:
|
| 632 |
-
# print(f'PIpeline skipping creation of model because model is given')
|
| 633 |
-
|
| 634 |
-
# if tokenizer.pad_token is None:
|
| 635 |
-
# tokenizer.pad_token_id = model.config.eos_token_id
|
| 636 |
-
|
| 637 |
-
# if (
|
| 638 |
-
# (
|
| 639 |
-
# getattr(model, "is_loaded_in_4bit", False)
|
| 640 |
-
# or getattr(model, "is_loaded_in_8bit", False)
|
| 641 |
-
# )
|
| 642 |
-
# and device is not None
|
| 643 |
-
# and backend == "default"
|
| 644 |
-
# ):
|
| 645 |
-
# logger.warning(
|
| 646 |
-
# f"Setting the `device` argument to None from {device} to avoid "
|
| 647 |
-
# "the error caused by attempting to move the model that was already "
|
| 648 |
-
# "loaded on the GPU using the Accelerate module to the same or "
|
| 649 |
-
# "another device."
|
| 650 |
-
# )
|
| 651 |
-
# device = None
|
| 652 |
-
|
| 653 |
-
# if (
|
| 654 |
-
# device is not None
|
| 655 |
-
# and importlib.util.find_spec("torch") is not None
|
| 656 |
-
# and backend == "default"
|
| 657 |
-
# ):
|
| 658 |
-
# import torch
|
| 659 |
-
|
| 660 |
-
# cuda_device_count = torch.cuda.device_count()
|
| 661 |
-
# if device < -1 or (device >= cuda_device_count):
|
| 662 |
-
# raise ValueError(
|
| 663 |
-
# f"Got device=={device}, "
|
| 664 |
-
# f"device is required to be within [-1, {cuda_device_count})"
|
| 665 |
-
# )
|
| 666 |
-
# if device_map is not None and device < 0:
|
| 667 |
-
# device = None
|
| 668 |
-
# if device is not None and device < 0 and cuda_device_count > 0:
|
| 669 |
-
# logger.warning(
|
| 670 |
-
# "Device has %d GPUs available. "
|
| 671 |
-
# "Provide device={deviceId} to `from_model_id` to use available"
|
| 672 |
-
# "GPUs for execution. deviceId is -1 (default) for CPU and "
|
| 673 |
-
# "can be a positive integer associated with CUDA device id.",
|
| 674 |
-
# cuda_device_count,
|
| 675 |
-
# )
|
| 676 |
-
# if device is not None and device_map is not None and backend == "openvino":
|
| 677 |
-
# logger.warning("Please set device for OpenVINO through: " "'model_kwargs'")
|
| 678 |
-
# if "trust_remote_code" in _model_kwargs:
|
| 679 |
-
# _model_kwargs = {
|
| 680 |
-
# k: v for k, v in _model_kwargs.items() if k != "trust_remote_code"
|
| 681 |
-
# }
|
| 682 |
-
# _pipeline_kwargs = pipeline_kwargs or {}
|
| 683 |
-
# pipeline = hf_pipeline(
|
| 684 |
-
# task=task,
|
| 685 |
-
# model=model,
|
| 686 |
-
# tokenizer=tokenizer,
|
| 687 |
-
# device=device,
|
| 688 |
-
# device_map=device_map,
|
| 689 |
-
# batch_size=batch_size,
|
| 690 |
-
# model_kwargs=_model_kwargs,
|
| 691 |
-
# **_pipeline_kwargs,
|
| 692 |
-
# )
|
| 693 |
-
# if pipeline.task not in VALID_TASKS:
|
| 694 |
-
# raise ValueError(
|
| 695 |
-
# f"Got invalid task {pipeline.task}, "
|
| 696 |
-
# f"currently only {VALID_TASKS} are supported"
|
| 697 |
-
# )
|
| 698 |
-
# return cls(
|
| 699 |
-
# pipeline=pipeline,
|
| 700 |
-
# model_id=model_id,
|
| 701 |
-
# model_kwargs=_model_kwargs,
|
| 702 |
-
# pipeline_kwargs=_pipeline_kwargs,
|
| 703 |
-
# batch_size=batch_size,
|
| 704 |
-
# **kwargs,
|
| 705 |
-
# )
|
| 706 |
-
|
| 707 |
-
# def _generate(
|
| 708 |
-
# self,
|
| 709 |
-
# prompts: List[str],
|
| 710 |
-
# stop: Optional[List[str]] = None,
|
| 711 |
-
# run_manager: Optional[CallbackManagerForLLMRun] = None,
|
| 712 |
-
# **kwargs: Any,
|
| 713 |
-
# ) -> LLMResult:
|
| 714 |
-
# # List to hold all results
|
| 715 |
-
# text_generations: List[str] = []
|
| 716 |
-
# pipeline_kwargs = kwargs.get("pipeline_kwargs", self.pipeline_kwargs)
|
| 717 |
-
# pipeline_kwargs = pipeline_kwargs if len(pipeline_kwargs) > 0 else self.pipeline_kwargs
|
| 718 |
-
# for i in range(0, len(prompts), self.batch_size):
|
| 719 |
-
# batch_prompts = prompts[i : i + self.batch_size]
|
| 720 |
-
# bos_token = self.pipeline.tokenizer.convert_ids_to_tokens(self.pipeline.tokenizer.bos_token_id)
|
| 721 |
-
# for i in range(len(batch_prompts)):
|
| 722 |
-
# if not batch_prompts[i].startswith(bos_token) and self.add_bos_token:
|
| 723 |
-
# batch_prompts[i] = bos_token + batch_prompts[i]
|
| 724 |
-
# # print(f'PROMPT: {stop=} {pipeline_kwargs=} ==================\n{batch_prompts[0]}\n==========================')
|
| 725 |
-
# # Process batch of prompts
|
| 726 |
-
# responses = self.pipeline(
|
| 727 |
-
# batch_prompts,
|
| 728 |
-
# **pipeline_kwargs,
|
| 729 |
-
# )
|
| 730 |
-
# # Process each response in the batch
|
| 731 |
-
# for j, (prompt, response) in enumerate(zip(batch_prompts, responses)):
|
| 732 |
-
# if isinstance(response, list):
|
| 733 |
-
# # if model returns multiple generations, pick the top one
|
| 734 |
-
# response = response[0]
|
| 735 |
-
# if self.pipeline.task == "text-generation":
|
| 736 |
-
# text = response["generated_text"]
|
| 737 |
-
# elif self.pipeline.task == "text2text-generation":
|
| 738 |
-
# text = response["generated_text"]
|
| 739 |
-
# elif self.pipeline.task == "summarization":
|
| 740 |
-
# text = response["summary_text"]
|
| 741 |
-
# elif self.pipeline.task in "translation":
|
| 742 |
-
# text = response["translation_text"]
|
| 743 |
-
# else:
|
| 744 |
-
# raise ValueError(
|
| 745 |
-
# f"Got invalid task {self.pipeline.task}, "
|
| 746 |
-
# f"currently only {VALID_TASKS} are supported"
|
| 747 |
-
# )
|
| 748 |
-
# # Append the processed text to results
|
| 749 |
-
# if text.startswith(prompt):
|
| 750 |
-
# text = text[len(prompt):]
|
| 751 |
-
# if stop is not None and any(x in text for x in stop):
|
| 752 |
-
# text = text[:text.index(stop[0])]
|
| 753 |
-
# # print(f">>{text}")
|
| 754 |
-
# text_generations.append(text)
|
| 755 |
-
# return LLMResult(
|
| 756 |
-
# generations=[[Generation(text=text)] for text in text_generations]
|
| 757 |
-
# )
|
| 758 |
-
|
|
|
|
| 144 |
# List to hold all results
|
| 145 |
text_generations: List[str] = []
|
| 146 |
stop_strings = stop
|
| 147 |
+
print(f'Pipeline run: {len(prompts)}')
|
| 148 |
for i in range(0, len(prompts), self.batch_size):
|
| 149 |
batch_prompts = prompts[i : i + self.batch_size]
|
| 150 |
responses = []
|
|
|
|
| 157 |
text = text[len(prompt):]
|
| 158 |
if stop is not None and any(x in text for x in stop):
|
| 159 |
text = text[:text.index(stop[0])]
|
|
|
|
| 160 |
text_generations.append(text)
|
| 161 |
return LLMResult(
|
| 162 |
generations=[[Generation(text=text)] for text in text_generations]
|
|
|
|
| 456 |
def create_web_search_engine(model_engine=None):
|
| 457 |
# from langchain_community.tools.tavily_search import TavilySearchResults
|
| 458 |
if model_engine is None:
|
| 459 |
+
raise ValueError(f'model_engine empty')
|
|
|
|
| 460 |
|
| 461 |
from langchain_core.utils.function_calling import (
|
| 462 |
convert_to_openai_function,
|
|
|
|
| 471 |
|
| 472 |
tools = [NewTavilySearchResults(max_results=1)]
|
| 473 |
formatted_tools = [convert_to_openai_tool(tool) for tool in tools]
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
multipurpose_chatbot/demos/websearch_chat_interface.py
CHANGED
|
@@ -115,8 +115,8 @@ def chat_web_search_response_stream_multiturn_engine(
|
|
| 115 |
if len(message) == 0:
|
| 116 |
raise gr.Error("The message cannot be empty!")
|
| 117 |
|
|
|
|
| 118 |
response_output = agent_executor.invoke({"input": message})
|
| 119 |
-
print(response_output)
|
| 120 |
response = response_output['output']
|
| 121 |
|
| 122 |
full_prompt = gradio_history_to_conversation_prompt(message.strip(), history=history, system_prompt=system_prompt)
|
|
@@ -217,6 +217,10 @@ class WebSearchChatInterfaceDemo(BaseDemo):
|
|
| 217 |
return demo_chat
|
| 218 |
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
"""
|
| 221 |
run
|
| 222 |
|
|
|
|
| 115 |
if len(message) == 0:
|
| 116 |
raise gr.Error("The message cannot be empty!")
|
| 117 |
|
| 118 |
+
print(f'Begin agent_invoke.')
|
| 119 |
response_output = agent_executor.invoke({"input": message})
|
|
|
|
| 120 |
response = response_output['output']
|
| 121 |
|
| 122 |
full_prompt = gradio_history_to_conversation_prompt(message.strip(), history=history, system_prompt=system_prompt)
|
|
|
|
| 217 |
return demo_chat
|
| 218 |
|
| 219 |
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
|
| 224 |
"""
|
| 225 |
run
|
| 226 |
|