Model: atasoglu/Turkish-Llama-3-8B-function-calling Source: Original Platform
165 lines
4.6 KiB
Markdown
165 lines
4.6 KiB
Markdown
---
|
||
base_model: ytu-ce-cosmos/Turkish-Llama-8b-DPO-v0.1
|
||
tags:
|
||
- text-generation-inference
|
||
- transformers
|
||
- unsloth
|
||
- llama
|
||
- trl
|
||
- sft
|
||
license: apache-2.0
|
||
language:
|
||
- en
|
||
- tr
|
||
datasets:
|
||
- atasoglu/turkish-function-calling-20k
|
||
pipeline_tag: text-generation
|
||
---
|
||
|
||
# Uploaded model
|
||
|
||
**This model was adapted from [ytu-ce-cosmos/Turkish-Llama-8b-DPO-v0.1](https://huggingface.co/ytu-ce-cosmos/Turkish-Llama-8b-DPO-v0.1) and fine-tuned on the [atasoglu/turkish-function-calling-20k](https://huggingface.co/datasets/atasoglu/turkish-function-calling-20k) dataset to perform function calling tasks in Turkish.**
|
||
|
||
- **Developed by:** atasoglu
|
||
- **License:** apache-2.0
|
||
- **Finetuned from model :** ytu-ce-cosmos/Turkish-Llama-8b-DPO-v0.1
|
||
|
||
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
||
|
||
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
||
|
||
# Usage
|
||
|
||
First, load the model:
|
||
|
||
```python
|
||
import json
|
||
from unsloth import FastLanguageModel
|
||
|
||
# loading the model and tokenizer
|
||
model, tokenizer = FastLanguageModel.from_pretrained(
|
||
model_name="atasoglu/Turkish-Llama-3-8B-function-calling",
|
||
load_in_4bit=True,
|
||
)
|
||
FastLanguageModel.for_inference(model)
|
||
```
|
||
|
||
Setup the tools and messages:
|
||
|
||
```python
|
||
# define the prompt templates
|
||
system_prompt = """Sen yardımsever, akıllı ve fonksiyon çağrısı yapabilen bir asistansın.
|
||
Aşağıda JSON parçası içinde verilen fonksiyonları kullanarak kullanıcının sorusunu uygun şekilde cevaplamanı istiyorum.
|
||
|
||
Fonksiyon çağrısı yaparken uyman gereken talimatlar:
|
||
|
||
* Fonksiyonlar, JSON şeması olarak ifade edilmiştir.
|
||
* Eğer kullanıcının sorusu, bu fonksiyonlardan en az biri kullanılarak cevaplanabiliyorsa; uygun bir fonksiyon çağrısını JSON parçası içinde oluştur.
|
||
* Fonksiyonların parametreleri için asla uydurmalar yapma ve sadece kullanıcının verdiği bilgileri kullan.
|
||
* Eğer kullanıcının sorusu herhangi bir fonksiyon ile cevaplanamıyorsa, sadece "Verilen fonksiyonlarla cevaplanamaz" metnini döndür ve başka bir açıklama yapma.
|
||
|
||
Bu talimatlara uyarak soruları cevaplandır."""
|
||
|
||
user_prompt = """### Fonksiyonlar
|
||
|
||
'''json
|
||
{tools}
|
||
'''
|
||
|
||
### Soru
|
||
|
||
{query}"""
|
||
|
||
# define the tools and messages
|
||
tools = [
|
||
{
|
||
"type": "function",
|
||
"function": {
|
||
"name": "get_weather",
|
||
"description": "Get current temperature for a given location.",
|
||
"parameters": {
|
||
"type": "object",
|
||
"properties": {
|
||
"location": {
|
||
"type": "string",
|
||
"description": "City and country e.g. Bogotá, Colombia",
|
||
}
|
||
},
|
||
"required": ["location"],
|
||
"additionalProperties": False,
|
||
},
|
||
"strict": True,
|
||
},
|
||
}
|
||
]
|
||
query = "Paris'te hava şu anda nasıl?"
|
||
messages = [
|
||
{
|
||
"role": "system",
|
||
"content": system_prompt,
|
||
},
|
||
{
|
||
"role": "user",
|
||
"content": user_prompt.format(
|
||
tools=json.dumps(tools, ensure_ascii=False),
|
||
query=query,
|
||
),
|
||
},
|
||
]
|
||
```
|
||
|
||
**NOTE:** Change the *single quote* character to a *backtick* in the user prompt before running to specify the JSON snippet.
|
||
|
||
Then, generate and evaluate the output:
|
||
|
||
```python
|
||
import re
|
||
|
||
|
||
# define an evaluation function
|
||
def eval_function_calling(text):
|
||
match_ = re.search(r"```json(.*)```", text, re.DOTALL)
|
||
if match_ is None:
|
||
return False, text
|
||
return True, json.loads(match_.group(1).strip())
|
||
|
||
|
||
# tokenize the inputs
|
||
inputs = tokenizer.apply_chat_template(
|
||
messages,
|
||
add_generation_prompt=True,
|
||
return_dict=True,
|
||
return_tensors="pt",
|
||
).to("cuda")
|
||
|
||
# define generation arguments
|
||
generation_kwargs = dict(
|
||
do_sample=True,
|
||
use_cache=True,
|
||
max_new_tokens=500,
|
||
temperature=0.3,
|
||
top_p=0.9,
|
||
top_k=40,
|
||
)
|
||
|
||
# finally, generate the output
|
||
outputs = model.generate(**inputs, **generation_kwargs)
|
||
output_ids = outputs[:, inputs["input_ids"].shape[1] :]
|
||
generated_texts = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
||
has_function_calling, results = eval_function_calling(generated_texts[0])
|
||
|
||
# print the model response
|
||
if has_function_calling:
|
||
for result in results:
|
||
fn = result["function"]
|
||
name, args = fn["name"], fn["arguments"]
|
||
print(f"Calling {name!r} function with these arguments: {args}")
|
||
else:
|
||
print(f"No function call: {results!r}")
|
||
```
|
||
|
||
Output:
|
||
|
||
```console
|
||
Calling 'get_weather' function with these arguments: {"location":"Paris, France"}
|
||
``` |