Views
No views yet
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3import torch.nn.functional as F
4
5prompt = """You are an assistant, and you need to call find appropriate functions according to the query of the users. Firstly, find the relevant functions, then get the function arguments by understanding the user's query. The following functions are available for you to fetch further data to answer user questions:
6
7Function:
8def basketapi_league_seasons(tournamentId):
9 '''
10 Get access to historical and current seasons for a specific basketball league using the tournament ID.
11 Args:
12 tournamentId (number): The argument tournamentId is a number that represents the identifier of a tournament in the context of the function.
13 '''
14
15def os_sports_goal_distributions(unique_tournament_id,season_id,team_id):
16 '''
17 Get goal distributions by team, tournament ID, and season ID for in-depth sports performance analysis.
18 Args:
19 unique_tournament_id (number): The unique_tournament_id argument is a number representing the unique identifier for a tournament.
20 season_id (number): The argument season_id is a number that represents the identifier of the season for the search query string.
21 team_id (number): The team_id argument represents the teams identification number.
22 '''
23
24def transfermarket_get_table(id,seasonID,domain,homeAway):
25 '''
26 Get tables by competition and season from transfermarket platform for comprehensive and detailed competition and season-related data.
27 Args:
28 id (string): The function argument "id" is a string representing an identifier.
29 seasonID (string): The seasonID argument is a string that represents the identifier for a specific season.
30 domain (string): The domain argument is a string that represents a search query.
31 homeAway (string): The homeAway argument is a string that represents the home or away status for a sports event.
32 '''
33
34def no_relevant_function():
35 '''
36 Call this when no other provided function can be called to answer the user query.
37 '''
38
39def soccersapi_stage_id(t,id):
40 '''
41 Get stage ID for a soccer match or event, access specific details like schedules, teams, and relevant data.
42 Args:
43 t (string): The argument "t" of type string represents the search query string.
44 id (number): This function argument is an identifier represented by a number, typically used to uniquely reference a specific entity within the system.
45 '''
46
47Request the complete season data for a recently established basketball league using the tournament ID 309, aiming to analyze its inaugural seasons.
48Response:
49"""
50
51class NexaGenerator:
52 def __init__(self, model_id: AutoModelForCausalLM, tokenizer_id: AutoTokenizer):
53 self.model = AutoModelForCausalLM.from_pretrained(
54 model_id, torch_dtype=torch.bfloat16, device_map="auto"
55 )
56 self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_id)
57 self.eos_token_id = self.tokenizer.eos_token_id
58 self.token2id = self.tokenizer.get_vocab()
59
60 def deterministic_generate_next_token(
61 self,
62 input_ids: torch.Tensor, # shape: (1, seq_len), no support for batch yet
63 add_conditional_mask: bool = False,
64 usable_token_ids: torch.tensor = None, # element is token id
65 ) -> torch.tensor:
66 if add_conditional_mask:
67 assert usable_token_ids is not None, "usable_token_ids is required"
68 next_logits = self.model(input_ids)["logits"][:, -1:]
69 if add_conditional_mask:
70 mask = torch.full_like(next_logits, float("-inf"))
71 mask.scatter_(
72 2,
73 usable_token_ids.unsqueeze(0).unsqueeze(0),
74 next_logits.gather(2, usable_token_ids.unsqueeze(0).unsqueeze(0)),
75 )
76 next_token_id = torch.argmax(mask, dim=-1)
77 else:
78 next_token_id = torch.argmax(next_logits, dim=-1)
79 return next_token_id
80
81nexa_generator = NexaGenerator(model_id="NexaAIDev/Octopus-v1", tokenizer_id="NexaAIDev/Octopus-v1")
82
83def get_response(prompt):
84 input_ids = nexa_generator.tokenizer(prompt, return_tensors="pt")["input_ids"].to("cuda")
85 for _ in range(200):
86 next_token_id = nexa_generator.deterministic_generate_next_token(
87 input_ids=input_ids,
88 add_conditional_mask=False,
89 usable_token_ids=None,
90 )
91 input_ids = torch.cat([input_ids, next_token_id], dim=-1)
92 if next_token_id[0].item() == nexa_generator.eos_token_id:
93 break
94 generated_text = nexa_generator.tokenizer.batch_decode(input_ids)
95 return generated_text[0]
96
97print(get_response(prompt))

@misc{gemma-2023-open-models,
author = {{Gemma Team, Google DeepMind}},
title = {Gemma: Open Models Based on Gemini Research and Technology},
url = {https://goo.gle/GemmaReport},
year = {2023},
}
@article{touvron2023llama,
title={Llama 2: Open foundation and fine-tuned chat models},
author={Touvron, Hugo and Martin, Louis and Stone, Kevin and Albert, Peter and Almahairi, Amjad and Babaei, Yasmine and Bashlykov, Nikolay and Batra, Soumya and Bhargava, Prajjwal and Bhosale, Shruti and others},
journal={arXiv preprint arXiv:2307.09288},
year={2023}
}
@misc{stable-code-3b,
author = {Pinnaparaju, Nikhil and Adithyan, Reshinth and Phung, Duy and Tow, Jonathan and Baicoianu, James and Cooper, Nathan},
title = {Stable Code 3B},
url = {https://huggingface.co/stabilityai/stable-code-3b},
year = {2023}
}@misc{chen2024octopus,
title={Octopus: On-device language model for function calling of software APIs},
author={Wei Chen and Zhiyuan Li and Mingyuan Ma},
year={2024},
eprint={2404.01549},
archivePrefix={arXiv},
primaryClass={cs.CL}
}