File size: 6,558 Bytes
e11a899
3a69f52
 
 
 
 
7cc0a2d
 
3d52f13
7cc0a2d
 
 
 
3a69f52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d52f13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a69f52
 
 
 
 
 
 
 
 
 
 
 
fa5922d
3a69f52
 
 
 
 
 
 
 
 
 
 
 
 
fa5922d
3a69f52
 
 
 
 
 
 
 
 
 
 
 
3d52f13
 
 
 
 
 
 
 
 
077a076
3d52f13
 
 
 
 
 
 
 
 
 
d1919a7
3d52f13
 
 
 
 
 
 
 
 
 
3a69f52
 
3d52f13
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
import spaces
import json
import torch
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

title = """# 🙋🏻‍♂️ Welcome to Tonic's Salesforce/Xlam-7B-r"""
description = """
🎬 Large Action Models (LAMs) are advanced large language models designed to enhance decision-making and translate user intentions into executable actions that interact with the world. LAMs autonomously plan and execute tasks to achieve specific goals, serving as the brains of AI agents. They have the potential to automate workflow processes across various domains, making them invaluable for a wide range of applications.Check our the Salesforce/xLAM models : [🤗 xLAM-1b-fc-r](https://huggingface.co/Salesforce/xLAM-1b-fc-r) | [🤗 xLAM-1b-fc-r-GGUF](https://huggingface.co/Salesforce/xLAM-1b-fc-r-gguf) [🤗 xLAM-7b-fc-r](https://huggingface.co/Salesforce/xLAM-7b-fc-r) | [🤗 xLAM-7b-fc-r-GGUF](https://huggingface.co/Salesforce/xLAM-7b-fc-r-gguf) [🤗 xLAM-7b-r ](https://huggingface.co/Salesforce/xLAM-7b-r) | [🤗 xLAM-8x7b-r](https://huggingface.co/Salesforce/xLAM-8x7b-r) [🤗 xLAM-8x22b-r](https://huggingface.co/Salesforce/xLAM-8x22b-r) |
### Join us : 
🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/GWpVpekp) On 🤗Huggingface:[MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [Build Tonic](https://git.tonic-ai.com/)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
"""

# Load model and tokenizer
model_name = "Salesforce/xLAM-7b-r"
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Set random seed for reproducibility
torch.random.manual_seed(0)

# Task and format instructions
task_instruction = """
Based on the previous context and API request history, generate an API request or a response as an AI assistant.""".strip()

format_instruction = """
The output should be of the JSON format, which specifies a list of generated function calls. The example format is as follows, please make sure the parameter type is correct. If no function call is needed, please make 
tool_calls an empty list "[]".
```
{"thought": "the thought process, or an empty string", "tool_calls": [{"name": "api_name1", "arguments": {"argument1": "value1", "argument2": "value2"}}]}
```
""".strip()

# Example tools and query
example_tools = json.dumps([
    {
        "name": "get_weather",
        "description": "Get the current weather for a location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, New York"
                },
                "unit": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The unit of temperature to return"
                }
            },
            "required": ["location"]
        }
    },
    {
        "name": "search",
        "description": "Search for information on the internet",
        "parameters": {
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "The search query, e.g. 'latest news on AI'"
                }
            },
            "required": ["query"]
        }
    }
], indent=2)

example_query = "What's the weather like in New York in fahrenheit?"

def convert_to_xlam_tool(tools):
    if isinstance(tools, dict):
        return {
            "name": tools["name"],
            "description": tools["description"],
            "parameters": {k: v for k, v in tools["parameters"].get("properties", {}).items()}
        }
    elif isinstance(tools, list):
        return [convert_to_xlam_tool(tool) for tool in tools]
    else:
        return tools

def build_prompt(task_instruction: str, format_instruction: str, tools: list, query: str):
    prompt = f"[BEGIN OF TASK INSTRUCTION]\n{task_instruction}\n[END OF TASK INSTRUCTION]\n\n"
    prompt += f"[BEGIN OF AVAILABLE TOOLS]\n{json.dumps(tools)}\n[END OF AVAILABLE TOOLS]\n\n"
    prompt += f"[BEGIN OF FORMAT INSTRUCTION]\n{format_instruction}\n[END OF FORMAT INSTRUCTION]\n\n"
    prompt += f"[BEGIN OF QUERY]\n{query}\n[END OF QUERY]\n\n"
    return prompt

def generate_response(tools_input, query):
    try:
        tools = json.loads(tools_input)
    except json.JSONDecodeError:
        return "Error: Invalid JSON format for tools input."

    xlam_format_tools = convert_to_xlam_tool(tools)
    content = build_prompt(task_instruction, format_instruction, xlam_format_tools, query)

    messages = [
        {'role': 'user', 'content': content}
    ]

    inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
    outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
    agent_action = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)

    return agent_action

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown(title)
    gr.Markdown(description)
    
    with gr.Row():
        with gr.Column():
            tools_input = gr.Code(
                label="Available Tools (JSON format)",
                lines=20,
                value=example_tools,
                language='json'
            )
            query_input = gr.Textbox(
                label="User Query",
                lines=2,
                value=example_query
            )
            submit_button = gr.Button("Generate Response")
        
        with gr.Column():
            output = gr.Code(label="🎬 xLam :", lines=10, language="json")
    
    submit_button.click(generate_response, inputs=[tools_input, query_input], outputs=output)
    
    gr.Examples(
        examples=[
            [example_tools, "What's the weather like in San Francisco in celsius?"],
            [example_tools, "Search for the latest news on artificial intelligence"],
        ],
        inputs=[tools_input, query_input],
    )

if __name__ == "__main__":
    demo.launch()