Views
No views yet

Gorilla OpenFunctions extends Large Language Model(LLM) Chat Completion feature to formulate executable APIs call given natural language instructions and API context.
1! pip install transformers accelerate bitsandbytes
2
3import json
4import torch
5from transformers import pipeline
6
7def get_prompt(user_query: str, functions: list = []) -> str:
8 """
9 Generates a conversation prompt based on the user's query and a list of functions.
10
11 Parameters:
12 - user_query (str): The user's query.
13 - functions (list): A list of functions to include in the prompt.
14
15 Returns:
16 - str: The formatted conversation prompt.
17 """
18 if len(functions) == 0:
19 return f"USER: <<question>> {user_query}\nASSISTANT: "
20 functions_string = json.dumps(functions)
21 return f"USER: <<question>> {user_query} <<function>> {functions_string}\nASSISTANT: "
22
23# Pipeline setup
24pipe = pipeline(
25 "text-generation",
26 model="anakin87/gorilla-openfunctions-v0-sharded",
27 device_map="auto",
28 model_kwargs={"load_in_8bit":True, "torch_dtype":torch.float16},
29 max_new_tokens=128,
30 batch_size=16
31)
32
33# Example usage
34query: str = "Call me an Uber ride type \"Plus\" in Berkeley at zipcode 94704 in 10 minutes"
35functions = [
36 {
37 "name": "Uber Carpool",
38 "api_name": "uber.ride",
39 "description": "Find suitable ride for customers given the location, type of ride, and the amount of time the customer is willing to wait as parameters",
40 "parameters": [
41 {"name": "loc", "description": "Location of the starting place of the Uber ride"},
42 {"name": "type", "enum": ["plus", "comfort", "black"], "description": "Types of Uber ride user is ordering"},
43 {"name": "time", "description": "The amount of time in minutes the customer is willing to wait"}
44 ]
45 }
46]
47
48# Generate prompt and obtain model output
49prompt = get_prompt(query, functions=functions)
50output = pipe(prompt)
51
52print(output[0]['generated_text'].rpartition("ASSISTANT:")[-1].strip())
53# uber.ride(loc="berkeley", type="plus", time=10)