File size: 8,624 Bytes
7c2ec76
 
db8e834
6c8fb3a
fd81245
db8e834
6d7ed94
fd81245
 
998f311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c295a51
7c2ec76
 
 
 
 
6d7ed94
3c4359a
6d7ed94
 
6c8fb3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d7ed94
6c8fb3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dccb70c
 
 
 
 
 
7291a79
7c2ec76
 
 
 
 
 
 
 
6165f69
7c2ec76
6165f69
 
 
 
 
7c2ec76
6165f69
7c2ec76
6c8fb3a
7c2ec76
a20c934
 
 
 
 
 
 
 
 
 
 
 
7c2ec76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import InferenceClient
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import sqlite3
import json
#setup database
conn = sqlite3.connect('sfdc_la.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS sfdc_la (
    LAid TEXT PRIMARY KEY,
    Amount REAL,
    Tenure INTEGER,
    ROI REAL,
    Stage VARCHAR
)
''')
#inserting data into table
loan_data = [
    ('LA00001', 10000, 12, 5.5, 'Customer Onboarding'),
    ('LA00002', 15000, 24, 6.0, 'Credit Review'),
    ('LA00003', 20000, 36, 6.5, 'Loan Disbursal'),
    ('LA23455', 25000, 48, 7.0, 'OTC Clearance'),
    ('LA00005', 30000, 60, 7.5, 'Customer Onboarding')
]
cursor.executemany('''
INSERT INTO sfdc_la (LAid, Amount, Tenure, ROI, Stage)
VALUES (?, ?, ?, ?, ?)
''', loan_data)
conn.commit()
#conn.close()
"""
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
"""
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")

modelpath = "Salesforce/xLAM-1b-fc-r"
model = AutoModelForCausalLM.from_pretrained(modelpath, torch_dtype="auto", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(modelpath)

#=============prompt template and task instructions==============
# Please use our provided instruction prompt for best performance
task_instruction = """
You are an expert in composing functions. You are given a question and a set of possible functions. 
Based on the question, you will need to make one or more function/tool calls to achieve the purpose. 
If none of the functions can be used, point it out and refuse to answer. 
If the given question lacks the parameters required by the function, also point it out.
""".strip()
format_instruction = """
The output MUST strictly adhere to the following JSON format, and NO other text MUST be included.
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 '[]'.
```
{
    "tool_calls": [
    {"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},
    ... (more tool calls as required)
    ]
}
```
""".strip()##==output format
#=============APIs and Functions Metadata========================
get_weather_api = {
    "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"]
    }
}

search_api = {
    "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"]
    }
}

search_loanapplication = {
    "name": "searchLA",
    "description": "Search for Loan Application status",
    "parameters": {
        "type": "object",
        "properties": {
            "loan_application_id": {
                "type": "alphanumeric string",
                "description": "The unique identifier for a loan application, eg: LA1234"
            },
            "phone_number": {
                "type": "string",
                "description": "The phone number associated with the loan application"
            }
        },
        "required": ["loan_application_id", "phone_number"]
    }
}

openai_format_tools = [search_api, search_loanapplication, get_weather_api]
# Helper function to convert openai format tools to our more concise xLAM format
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
#=========prompt builder====================================
# Helper function to build the input prompt for our model
def build_prompt(task_instruction: str, format_instruction: str, xlam_format_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(xlam_format_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 to_model(query):
    xlam_format_tools = convert_to_xlam_tool(openai_format_tools)
    content = build_prompt(task_instruction, format_instruction, xlam_format_tools, query)
    #print(f"content: {content}")
    messages=[
        { 'role': 'user', 'content': content}
    ]
    inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
    # tokenizer.eos_token_id is the id of <|EOT|> token
    outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
    return (tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))

def to_app(callobj):
    callobject = json.loads(callobj)
    callfunctions = []
    callarguments = []
    for tool_call in callobject['tool_calls']:
        callfunctions.append(tool_call['name'])
        callarguments.append(list(tool_call['arguments'].values()))
    #print(f"fuctions: {callfunctions}")
    #print(f"arguments: {callarguments}")
    return callfunctions, callarguments
#===========sample application===================================
def application(callfunctions, callarguments):
    ##los application functions
    def get_weather(location):
        return (print(f"weather function executed with city {location}"))
    def searchLA(laid, phnumber):
        query = f"""SELECT * from sfdc_la where LAid = '{laid}'"""
        cursor.execute(query)
        result = cursor.fetchall()
        return (print(result))
    losfunctions_list = ['get_weather','searchLA']
    for i, functionname in enumerate(callfunctions):
        if functionname in losfunctions_list:
            function = globals().get(functionname) or locals().get(functionname)
            if function:
                arguments = callarguments[i]
                out = function(*arguments)
    return out
#out = application(callfunctions, callarguments)
def process_input(input_str):
    if not input_str:
        return "No input provided!"
    try:
        model_out = to_model(input_str)
        funs, args = to_app(model_out)
        output_obj = application(funs, args)
        return output_obj
    except Exception as e:
        return f"Error: {str(e)}"

def respond(
    message,
    history: list[tuple[str, str]],
    max_tokens,
    temperature,
    top_p,
):
    #messages = []

    #for val in history:
    #    if val[0]:
    #        messages.append({"role": "user", "content": val[0]})
    #    if val[1]:
    #        messages.append({"role": "assistant", "content": val[1]})

    #messages.append({"role": "user", "content": message})

    response = process_input(message)

    #for message in client.chat_completion(
    #    messages,
    #    max_tokens=max_tokens,
    #    stream=True,
    #    temperature=temperature,
    #    top_p=top_p,
    #):
    #    token = message.choices[0].delta.content

    #    response += token
    #    yield response
    return response

"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
demo = gr.ChatInterface(
    respond,
    additional_inputs=[
        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.95,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
    ],
)


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