Views
No views yet
meta-llama/Llama-3.2-1B-Instruct model enhanced with function/tool calling capabilities. The model leverages the hiyouga/glaive-function-calling-v2-sharegpt dataset for training.pip install langchain pydantic torch langchain-ollamaollama create <model_name> -f <path_to_modelfile>1from langchain_ollama import ChatOllama
2
3# Initialize model instance
4llm = ChatOllama(model="<model_name>")1# Arithmetic computation example
2query = "What is 3 * 12? Also, what is 11 + 49?"
3response = llm.invoke(query)
4
5print(response.content)
6# Output:
7# 1. 3 times 12 is 36.
8# 2. 11 plus 49 is 60.1from pydantic import BaseModel, Field
2
3class add(BaseModel):
4 """Addition operation for two integers."""
5 a: int = Field(..., description="First integer")
6 b: int = Field(..., description="Second integer")
7
8class multiply(BaseModel):
9 """Multiplication operation for two integers."""
10 a: int = Field(..., description="First integer")
11 b: int = Field(..., description="Second integer")
12
13# Tool registration
14tools = [add, multiply]
15llm_tools = llm.bind_tools(tools)
16
17# Execute query
18response = llm_tools.invoke(query)
19print(response.content)
20# Output:
21# {"type":"function","function":{"name":"multiply","arguments":[{"a":3,"b":12}]}}
22# {"type":"function","function":{"name":"add","arguments":[{"a":11,"b":49}}]}}1from pydantic import BaseModel, Field
2from typing import List, Optional
3
4class SendEmail(BaseModel):
5 """Send an email to specified recipients."""
6
7 to: List[str] = Field(..., description="List of email recipients")
8 subject: str = Field(..., description="Email subject")
9 body: str = Field(..., description="Email content/body")
10 cc: Optional[List[str]] = Field(None, description="CC recipients")
11 attachments: Optional[List[str]] = Field(None, description="List of attachment file paths")
12
13class WeatherInfo(BaseModel):
14 """Get weather information for a specific location."""
15
16 city: str = Field(..., description="City name")
17 country: Optional[str] = Field(None, description="Country name")
18 units: str = Field("celsius", description="Temperature units (celsius/fahrenheit)")
19
20class SearchWeb(BaseModel):
21 """Search the web for given query."""
22
23 query: str = Field(..., description="Search query")
24 num_results: int = Field(5, description="Number of results to return")
25 language: str = Field("en", description="Search language")
26
27class CreateCalendarEvent(BaseModel):
28 """Create a calendar event."""
29
30 title: str = Field(..., description="Event title")
31 start_time: str = Field(..., description="Event start time (ISO format)")
32 end_time: str = Field(..., description="Event end time (ISO format)")
33 description: Optional[str] = Field(None, description="Event description")
34 attendees: Optional[List[str]] = Field(None, description="List of attendee emails")
35
36class TranslateText(BaseModel):
37 """Translate text between languages."""
38
39 text: str = Field(..., description="Text to translate")
40 source_lang: str = Field(..., description="Source language code (e.g., 'en', 'es')")
41 target_lang: str = Field(..., description="Target language code (e.g., 'fr', 'de')")
42
43class SetReminder(BaseModel):
44 """Set a reminder for a specific time."""
45
46 message: str = Field(..., description="Reminder message")
47 time: str = Field(..., description="Reminder time (ISO format)")
48 priority: str = Field("normal", description="Priority level (low/normal/high)")
49
50# Combine all tools
51tools = [
52 SendEmail,
53 WeatherInfo,
54 SearchWeb,
55 CreateCalendarEvent,
56 TranslateText,
57 SetReminder
58]
59llm_tools = llm.bind_tools(tools)
60
61# Example usage
62query = "Set a reminder to call John at 3 PM tomorrow. Also, translate 'Hello, how are you?' to Spanish."
63print(llm_tools.invoke(query).content)
64# Output:
65# {"type":"function","function":{"name":"SetReminder","arguments":{"message":"Call John at 3 PM tomorrow"},"arguments":{"time":"","priority":"normal"}}}
66# {"type":"function","function":{"name":"TranslateText","arguments":{"text":"Hello, how are you?", "source_lang":"en", "target_lang":"es"}}hiyouga/glaive-function-calling-v2-sharegpt dataset, featuring comprehensive function calling interaction examples.1@misc{function-calling-llama,
2 author = {nguyenthanhthuan_banhmi},
3 title = {Function Calling Llama Model Vesion 1},
4 year = {2024},
5 publisher = {GitHub},
6 journal = {GitHub repository}
7}