Views
No views yet
meta-llama/Llama-3.2-1B-Instruct model enhanced with function/tool calling capabilities. The model leverages the nguyenthanhthuan/function-calling-sharegpt dataset for training.pip install langchain pydantic torch langchain-ollama langchain_coreollama 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
2
3# Note that the docstrings here are crucial, as they will be passed along
4# to the model along with the class name.
5class add(BaseModel):
6 """Add two integers together."""
7
8 a: int = Field(..., description="First integer")
9 b: int = Field(..., description="Second integer")
10
11class multiply(BaseModel):
12 """Multiply two integers together."""
13
14 a: int = Field(..., description="First integer")
15 b: int = Field(..., description="Second integer")
16
17tools = [add, multiply]
18llm_with_tools = llm.bind_tools(tools)
19
20# Execute query and parser result (Different from the first version)
21from langchain_core.output_parsers.openai_tools import PydanticToolsParser
22
23query = "What is 3 * 12? Also, what is 11 + 49?"
24chain = llm_with_tools | PydanticToolsParser(tools=tools)
25result = chain.invoke(query)
26print(result)
27
28# Output:
29# [multiply(a=3, b=12), add(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)")
49tools = [
50 SendEmail,
51 WeatherInfo,
52 SearchWeb,
53 CreateCalendarEvent,
54 TranslateText,
55 SetReminder
56]
57llm_tools = llm.bind_tools(tools)
58
59# # Execute query and parser result (Different from the first version)
60from langchain_core.output_parsers.openai_tools import PydanticToolsParser
61
62query = "Set a reminder to call John at 3 PM tomorrow. Also, translate 'Hello, how are you?' to Spanish."
63chain = llm_tools | PydanticToolsParser(tools=tools)
64result = chain.invoke(query)
65print(result)
66
67# Output:
68# [SetReminder(message='Set a reminder for a specific time.', time='3 PM tomorrow', priority='normal'),
69# TranslateText(text='Hello, how are you?', source_lang='en', target_lang='es')]nguyenthanhthuan/function-calling-sharegpt dataset, featuring comprehensive function calling interaction examples.1@misc{function-calling-llama,
2 author = {nguyenthanhthuan_banhmi},
3 title = {Function Calling Llama Model Version 2} ,
4 year = {2024},
5 publisher = {GitHub},
6 journal = {GitHub repository}
7}