legolasyiu commited on
Commit
87542db
1 Parent(s): eb457e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -1
app.py CHANGED
@@ -4,6 +4,82 @@ import torch
4
 
5
  import gradio as gr
6
 
7
- demo = gr.load("EpistemeAI/Fireball-Meta-Llama-3.1-8B-Instruct-Agent-0.003-128K-code", src="models")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  demo.launch()
 
4
 
5
  import gradio as gr
6
 
7
+ from langchain import hub
8
+ from langchain.agents import AgentExecutor, create_openai_tools_agent, load_tools
9
+ from langchain_openai import ChatOpenAI
10
+ from gradio import ChatMessage
11
+ import gradio as gr
12
+
13
+ from dotenv import load_dotenv
14
+
15
+ load_dotenv()
16
+ # Environment variables
17
+ HF_TOKEN = os.environ.get('HF_TOKEN') # Ensure token is set
18
+ #model = ChatOpenAI(temperature=0, streaming=True)
19
+
20
+ from langchain_community.llms import HuggingFaceEndpoint
21
+ from langchain_community.chat_models.huggingface import ChatHuggingFace
22
+
23
+ from transformers import BitsAndBytesConfig
24
+ #quantization to 8bit, must have GPU.
25
+ quantization_config = BitsAndBytesConfig(
26
+ load_in_4bit=True,
27
+ bnb_4bit_quant_type="nf4",
28
+ bnb_4bit_compute_dtype="float16",
29
+ bnb_4bit_use_double_quant=True,
30
+ )
31
+
32
+ # 2. Create model
33
+ model = HuggingFacePipeline.from_model_id(
34
+ model_id="EpistemeAI/Fireball-Meta-Llama-3.1-8B-Instruct-Agent-0.003",
35
+ task="text-generation",
36
+ pipeline_kwargs=dict(
37
+ max_new_tokens=2048,
38
+ do_sample=False,
39
+ repetition_penalty=1.03,
40
+ return_full_text=False,
41
+ ),
42
+ model_kwargs={"quantization_config": quantization_config},
43
+ )
44
+
45
+ tools = load_tools(["serpapi"])
46
+
47
+ # Get the prompt to use - you can modify this!
48
+ prompt = hub.pull("hwchase17/openai-tools-agent")
49
+ # print(prompt.messages) -- to see the prompt
50
+ agent = create_openai_tools_agent(
51
+ model.with_config({"tags": ["agent_llm"]}), tools, prompt
52
+ )
53
+ agent_executor = AgentExecutor(agent=agent, tools=tools).with_config(
54
+ {"run_name": "Agent"}
55
+ )
56
+ async def interact_with_langchain_agent(prompt, messages):
57
+ messages.append(ChatMessage(role="user", content=prompt))
58
+ yield messages
59
+ async for chunk in agent_executor.astream(
60
+ {"input": prompt}
61
+ ):
62
+ if "steps" in chunk:
63
+ for step in chunk["steps"]:
64
+ messages.append(ChatMessage(role="assistant", content=step.action.log,
65
+ metadata={"title": f"🛠️ Used tool {step.action.tool}"}))
66
+ yield messages
67
+ if "output" in chunk:
68
+ messages.append(ChatMessage(role="assistant", content=chunk["output"]))
69
+ yield messages
70
+
71
+
72
+ with gr.Blocks() as demo:
73
+ gr.Markdown("# Chat with a LangChain Agent 🦜⛓️ and see its thoughts 💭")
74
+ chatbot = gr.Chatbot(
75
+ type="messages",
76
+ label="Agent",
77
+ avatar_images=(
78
+ None,
79
+ "https://em-content.zobj.net/source/twitter/141/parrot_1f99c.png",
80
+ ),
81
+ )
82
+ input = gr.Textbox(lines=1, label="Chat Message")
83
+ input.submit(interact_with_langchain_agent, [input_2, chatbot_2], [chatbot_2])
84
 
85
  demo.launch()