当前位置:   article > 正文

LaVague 本地调用 Ollam, 使用llama3_lavague怎么使用

lavague怎么使用

Ollama完成本地模型的运行-CSDN博客

LLM的测试工具:LaVague平替成国内大模型_llm的测试工具:lavague-CSDN博客

首先,对社区大佬CrissChan表示感谢,基于上面两个文章我进行了一些尝试
 

  1. # import locale
  2. import gradio as gr
  3. from llama_index.core import Document
  4. from llama_index.core import PromptTemplate
  5. from llama_index.core import VectorStoreIndex
  6. from llama_index.core import get_response_synthesizer
  7. # Monkey patch because stream_complete is not implemented in the current version of llama_index
  8. from llama_index.core.base.llms.types import (
  9. CompletionResponse,
  10. )
  11. from llama_index.core.node_parser import CodeSplitter
  12. from llama_index.core.query_engine import RetrieverQueryEngine
  13. from llama_index.core.response_synthesizers import ResponseMode
  14. from llama_index.retrievers.bm25 import BM25Retriever
  15. from selenium import webdriver
  16. from selenium.webdriver.chrome.options import Options
  17. from selenium.webdriver.chrome.service import Service
  18. from selenium.webdriver.common.by import By
  19. # from action_engine import ActionEngine
  20. from llama_index.embeddings.ollama import OllamaEmbedding
  21. from llama_index.llms.ollama import Ollama
  22. # locale.getpreferredencoding = lambda: "UTF-8"
  23. ## chatglm modle
  24. llm = Ollama(model='llama3', request_timeout=2000, )
  25. # embed_model = "llama3"
  26. embedder = OllamaEmbedding(model_name="llama3", )
  27. with open("prompt_template.txt", "r") as file:
  28. PROMPT_TEMPLATE_STR = file.read()
  29. # Preparing the action engine
  30. MAX_CHARS = 1500
  31. K = 3
  32. class ActionEngine:
  33. def __init__(self, llm, embedding):
  34. self.llm = llm
  35. self.embedding = embedding
  36. def _get_index(self, html):
  37. text_list = [html]
  38. documents = [Document(text=t) for t in text_list]
  39. splitter = CodeSplitter(
  40. language="html",
  41. chunk_lines=40, # lines per chunk
  42. chunk_lines_overlap=200, # lines overlap between chunks
  43. max_chars=MAX_CHARS, # max chars per chunk
  44. )
  45. nodes = splitter.get_nodes_from_documents(documents)
  46. nodes = [node for node in nodes if node.text]
  47. index = VectorStoreIndex(nodes, embed_model=self.embedding)
  48. return index
  49. def get_query_engine(self, state):
  50. html = state
  51. index = self._get_index(html)
  52. retriever = BM25Retriever.from_defaults(
  53. index=index,
  54. similarity_top_k=K,
  55. )
  56. response_synthesizer = get_response_synthesizer(streaming=True, llm=self.llm)
  57. # assemble query engine
  58. query_engine = RetrieverQueryEngine(
  59. retriever=retriever,
  60. response_synthesizer=response_synthesizer,
  61. )
  62. prompt_template = PromptTemplate(PROMPT_TEMPLATE_STR)
  63. query_engine.update_prompts(
  64. {"response_synthesizer:text_qa_template": prompt_template}
  65. )
  66. return query_engine
  67. # Code execution in action
  68. action_engine = ActionEngine(llm, embedder)
  69. ## Setup chrome options
  70. chrome_options = Options()
  71. # chrome_options.add_argument("--headless") # Ensure GUI is off
  72. chrome_options.add_argument("--no-sandbox")
  73. chrome_options.add_argument("--window-size=1600,900")
  74. # webdriver_service = Service("chromedriver")
  75. # Set path to chrome/chromedriver as per your configuration
  76. # try:
  77. # import google.colab
  78. # chrome_options.binary_location = "/Users/crisschan/workspace/PySpace/laVague/content/chrome-linux64/chrome"
  79. webdriver_service = Service("{driverHome}\\chromedriver.exe")
  80. # except:
  81. # import os.path
  82. # homedir = os.path.expanduser("~")
  83. # chrome_options.binary_location = f"{homedir}/chrome-linux64/chrome"
  84. # webdriver_service = Service(f"{homedir}/chromedriver-linux64/chromedriver")
  85. # Choose Chrome Browser
  86. driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)
  87. title = """
  88. <div align="center">
  89. <h1> Welcome to Ollama3-LaVague</h1>
  90. <p>Redefining internet surfing by transforming natural language instructions into seamless browser interactions.</p>
  91. </div>
  92. """
  93. # action_engine = ActionEngine(llm, embedder)
  94. def process_url(url):
  95. driver.get(url)
  96. driver.save_screenshot("screenshot.png")
  97. # This function is supposed to fetch and return the image from the URL.
  98. # Placeholder function: replace with actual image fetching logic.
  99. return "screenshot.png"
  100. def process_instruction(query):
  101. state = driver.page_source
  102. query_engine = action_engine.get_query_engine(state)
  103. streaming_response = query_engine.query(query)
  104. source_nodes = streaming_response.get_formatted_sources(MAX_CHARS)
  105. response = ""
  106. for text in streaming_response.response_gen:
  107. # do something with text as they arrive.
  108. print(text)
  109. response += text
  110. yield response, source_nodes
  111. def exec_code(code):
  112. code = "#" + code.split("```")[1]
  113. # print(code)
  114. try:
  115. # if len(code)==0:
  116. # return "No code Generated"
  117. # else:
  118. exec(code)
  119. print(code)
  120. return "Successful code execution", code
  121. except Exception as e:
  122. output = f"Error in code execution: {str(e)}"
  123. return output, code
  124. def update_image_display(img):
  125. driver.save_screenshot("screenshot.png")
  126. url = driver.current_url
  127. return "screenshot.png", url
  128. def create_demo(base_url, instructions):
  129. with gr.Blocks() as demo:
  130. with gr.Row():
  131. gr.HTML(title)
  132. with gr.Row():
  133. url_input = gr.Textbox(value=base_url, label="Enter URL and press 'Enter' to load the page.")
  134. with gr.Row():
  135. with gr.Column(scale=8):
  136. image_display = gr.Image(label="Browser", interactive=False)
  137. with gr.Column(scale=2):
  138. text_area = gr.Textbox(label="Instructions")
  139. gr.Examples(examples=instructions, inputs=text_area,
  140. )
  141. generate_btn = gr.Button(value="Execute")
  142. code_display = gr.Code(label="Generated code", language="python",
  143. lines=5, interactive=False)
  144. with gr.Accordion(label="Logs", open=False) as log_accordion:
  145. log_display = gr.Textbox(interactive=False)
  146. source_display = gr.Textbox(label="Retrieved nodes", interactive=False)
  147. # Linking components
  148. url_input.submit(process_url, inputs=url_input, outputs=image_display)
  149. generate_btn.click(process_instruction, inputs=text_area, outputs=[code_display, source_display]).then(
  150. exec_code, inputs=code_display, outputs=[log_display, code_display]
  151. ).then(
  152. update_image_display, inputs=image_display, outputs=[image_display, url_input]
  153. )
  154. demo.launch(share=False)
  155. if __name__ == "__main__":
  156. base_url = "http://127.0.0.1:7860/"
  157. instructions = [
  158. "Enter test1234@google.com in the first input box,Then enter test1234 in the second input box, and then click SIGN IN button,Wait for the new page to load",
  159. "Click the APPS icon under Apps on the page, Wait for the new page to load",
  160. "Wait for the page element Add New to appear,then click Add New button",
  161. "Click Experiment that under Add New, Wait for the new page to load"]
  162. create_demo(base_url, instructions)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/785786
推荐阅读
相关标签
  

闽ICP备14008679号