Views
No views yet
| model | functionality |
|---|---|
| gorilla-openfunctions-v0 | Given a function, and user intent, returns properly formatted json with the right arguments |
| gorilla-openfunctions-v1 | + Parallel functions, and can choose between functions |
!pip install openai==0.28.11import openai
2
3def get_gorilla_response(prompt="Call me an Uber ride type \"Plus\" in Berkeley at zipcode 94704 in 10 minutes", model="gorilla-openfunctions-v0", functions=[]):
4 openai.api_key = "EMPTY"
5 openai.api_base = "http://luigi.millennium.berkeley.edu:8000/v1"
6 try:
7 completion = openai.ChatCompletion.create(
8 model="gorilla-openfunctions-v0",
9 temperature=0.0,
10 messages=[{"role": "user", "content": prompt}],
11 functions=functions,
12 )
13 return completion.choices[0].message.content
14 except Exception as e:
15 print(e, model, prompt)1query = "Call me an Uber ride type \"Plus\" in Berkeley at zipcode 94704 in 10 minutes"
2functions = [
3 {
4 "name": "Uber Carpool",
5 "api_name": "uber.ride",
6 "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",
7 "parameters": [{"name": "loc", "description": "location of the starting place of the uber ride"}, {"name":"type", "enum": ["plus", "comfort", "black"], "description": "types of uber ride user is ordering"}, {"name": "time", "description": "the amount of time in minutes the customer is willing to wait"}]
8 }
9]
10get_gorilla_response(query, functions=functions)uber.ride(loc="berkeley", type="plus", time=10)1import json
2import torch
3from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
4
5def get_prompt(user_query: str, functions: list = []) -> str:
6 """
7 Generates a conversation prompt based on the user's query and a list of functions.
8
9 Parameters:
10 - user_query (str): The user's query.
11 - functions (list): A list of functions to include in the prompt.
12
13 Returns:
14 - str: The formatted conversation prompt.
15 """
16 if len(functions) == 0:
17 return f"USER: <<question>> {user_query}\nASSISTANT: "
18 functions_string = json.dumps(functions)
19 return f"USER: <<question>> {user_query} <<function>> {functions_string}\nASSISTANT: "
20
21# Device setup
22device : str = "cuda:0" if torch.cuda.is_available() else "cpu"
23torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
24
25# Model and tokenizer setup
26model_id : str = "gorilla-llm/gorilla-openfunctions-v0"
27tokenizer = AutoTokenizer.from_pretrained(model_id)
28model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True)
29
30# Move model to device
31model.to(device)
32
33# Pipeline setup
34pipe = pipeline(
35 "text-generation",
36 model=model,
37 tokenizer=tokenizer,
38 max_new_tokens=128,
39 batch_size=16,
40 torch_dtype=torch_dtype,
41 device=device,
42)
43
44# Example usage
45query: str = "Call me an Uber ride type \"Plus\" in Berkeley at zipcode 94704 in 10 minutes"
46functions = [
47 {
48 "name": "Uber Carpool",
49 "api_name": "uber.ride",
50 "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",
51 "parameters": [
52 {"name": "loc", "description": "Location of the starting place of the Uber ride"},
53 {"name": "type", "enum": ["plus", "comfort", "black"], "description": "Types of Uber ride user is ordering"},
54 {"name": "time", "description": "The amount of time in minutes the customer is willing to wait"}
55 ]
56 }
57]
58
59# Generate prompt and obtain model output
60prompt = get_prompt(query, functions=functions)
61output = pipe(prompt)
62
63print(output)