कई फंक्शनों के साथ काम करना

OpenAI API के साथ AI सिस्टम बनाना

Francesca Donadoni

Curriculum Manager, DataCamp

पैरलल फंक्शन कॉलिंग

फंक्शन कॉलिंग का उपयोग करके OpenAI API कॉल का एक डायग्राम

OpenAI API के साथ AI सिस्टम बनाना

पैरलल फंक्शन कॉलिंग

कई फंक्शनों के साथ फंक्शन कॉलिंग द्वारा OpenAI API कॉल का एक डायग्राम

OpenAI API के साथ AI सिस्टम बनाना

उदाहरण संदेश

 

function_definition = [{'type': 'function','function':
        {'name': 'extract_job_info',
        'description': 'Get the job information from the body of the input text',
        'parameters': {
            'type': 'object',
            'properties': {
                'job': {'type': 'string', 'description': 'Job title'},
                'location': {'type': 'string', 'description': 'Location'}}
        }}
    }]
OpenAI API के साथ AI सिस्टम बनाना

उदाहरण संदेश

function_definition.append({'type': 'function',
        'function':{
            'name': 'get_timezone',
            'description': 'Return the timezone corresponding to the location in the 
                            job advert',
            'parameters': {
            'type': 'object',
            'properties': {
                'timezone': {'type': 'string','description': 'Timezone'}}}
        }
    }
)
OpenAI API के साथ AI सिस्टम बनाना

उदाहरण संदेश

 

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
    tools=function_definition
)
OpenAI API के साथ AI सिस्टम बनाना

उदाहरण प्रतिक्रिया

 

# Print the arguments of the first function
print(response.choices[0].message.tool_calls[0].function.arguments)
{"job": "Data Scientist", "location": "San Francisco, CA"}
# Print the arguments of the second function
print(response.choices[0].message.tool_calls[1].function.arguments)
{"timezone": "America/Los_Angeles"}
OpenAI API के साथ AI सिस्टम बनाना

विशिष्ट फंक्शन सेट करना

 

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
    tools=function_definition,

tool_choice='auto'
)
OpenAI API के साथ AI सिस्टम बनाना

विशिष्ट फंक्शन सेट करना

 

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
    tools=function_definition,
    tool_choice={'type': 'function', 
                         'function': {'name': 'extract_job_info'}
                 }
)
OpenAI API के साथ AI सिस्टम बनाना

प्रतिक्रिया को दोबारा जाँचना

 

messages = []
messages.append({"role": "system", "content": "Don't make assumptions about what 
values to plug into functions. Don't make up values to fill the response with."})
messages.append({"role": "system", "content": "Ask for clarification if needed."})
messages.append({"role": "user", "content": "What is the starting salary 
                                             for the role?"})
OpenAI API के साथ AI सिस्टम बनाना

प्रतिक्रिया को दोबारा जाँचना

 

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
    tools=function_definition
)
print(response.choices[0].message.content)
I don't have information about the starting salary for the role. If you'd like, I 
can help you extract the job information from the description and then provide 
additional assistance.
OpenAI API के साथ AI सिस्टम बनाना

अभ्यास करते हैं!

OpenAI API के साथ AI सिस्टम बनाना

Preparing Video For Download...