response = client.chat.completions.create( messages=[ { "role": "system", "content": "You are a helpful assistant.", }, { "role": "user", "content": "What is the capital of France?", }, { "role": "assistant", "content": "The capital of France is Paris.", }, { "role": "user", "content": "What about Spain?", } ], model=model_name, )
response = client.chat.completions.create( messages=[ { "role": "system", "content": "You are a helpful assistant.", }, { "role": "user", "content": "Give me 5 good reasons why I should exercise every day.", } ], model=model_name, stream=True )
for update in response: if update.choices[0].delta.content: print(update.choices[0].delta.content, end="")
defget_image_data_url(image_file: str, image_format: str) -> str: """ Helper function to converts an image file to a data URL string. Args: image_file (str): The path to the image file. image_format (str): The format of the image file. Returns: str: The data URL of the image. """ try: withopen(image_file, "rb") as f: image_data = base64.b64encode(f.read()).decode("utf-8") except FileNotFoundError: print(f"Could not read '{image_file}'.") exit() returnf"data:image/{image_format};base64,{image_data}"
# Define a function that returns flight information between two cities (mock implementation) defget_flight_info(origin_city: str, destination_city: str): if origin_city == "Seattle"and destination_city == "Miami": return json.dumps({ "airline": "Delta", "flight_number": "DL123", "flight_date": "May 7th, 2024", "flight_time": "10:00AM"}) return json.dumps({"error": "No flights found between the cities"})
# Define a function tool that the model can ask to invoke in order to retrieve flight information tool={ "type": "function", "function": { "name": "get_flight_info", "description": """Returns information about the next flight between two cities. This includes the name of the airline, flight number and the date and time of the next flight""", "parameters": { "type": "object", "properties": { "origin_city": { "type": "string", "description": "The name of the city where the flight originates", }, "destination_city": { "type": "string", "description": "The flight destination city", }, }, "required": [ "origin_city", "destination_city" ], }, }, }
messages=[ {"role": "system", "content": "You an assistant that helps users find flight information."}, {"role": "user", "content": "I'm interested in going to Miami. What is the next flight there from Seattle?"}, ]
# We expect the tool to be a function call if tool_call.type == "function":
# Parse the function call arguments and call the function function_args = json.loads(tool_call.function.arguments.replace("'", '"')) print(f"Calling function `{tool_call.function.name}` with arguments {function_args}") callable_func = locals()[tool_call.function.name] function_return = callable_func(**function_args) print(f"Function returned = {function_return}")
# Append the function call result fo the chat history messages.append( { "tool_call_id": tool_call.id, "role": "tool", "name": tool_call.function.name, "content": function_return, } )
# Get another response from the model response = client.chat.completions.create( messages=messages, tools=[tool], model=model_name, )
import os from azure.ai.inference import ChatCompletionsClient from azure.ai.inference.models import SystemMessage, UserMessage from azure.core.credentials import AzureKeyCredential
response = client.complete( messages=[ SystemMessage(content="You are a helpful assistant."), UserMessage(content="What is the capital of France?"), ], model=model_name, temperature=1.0, max_tokens=1000, top_p=1.0 )
import os from azure.ai.inference import ChatCompletionsClient from azure.ai.inference.models import AssistantMessage, SystemMessage, UserMessage from azure.core.credentials import AzureKeyCredential
messages = [ SystemMessage(content="You are a helpful assistant."), UserMessage(content="What is the capital of France?"), AssistantMessage(content="The capital of France is Paris."), UserMessage(content="What about Spain?"), ]
import os from azure.ai.inference import ChatCompletionsClient from azure.ai.inference.models import SystemMessage, UserMessage from azure.core.credentials import AzureKeyCredential
response = client.complete( stream=True, messages=[ SystemMessage(content="You are a helpful assistant."), UserMessage(content="Give me 5 good reasons why I should exercise every day."), ], model=model_name, )
for update in response: if update.choices: print(update.choices[0].delta.content or"", end="")
# Define a function that returns flight information between two cities (mock implementation) defget_flight_info(origin_city: str, destination_city: str): if origin_city == "Seattle"and destination_city == "Miami": return json.dumps({ "airline": "Delta", "flight_number": "DL123", "flight_date": "May 7th, 2024", "flight_time": "10:00AM"}) return json.dumps({"error": "No flights found between the cities"})
# Define a function tool that the model can ask to invoke in order to retrieve flight information flight_info = ChatCompletionsToolDefinition( function=FunctionDefinition( name="get_flight_info", description="""Returns information about the next flight between two cities. This includes the name of the airline, flight number and the date and time of the next flight""", parameters={ "type": "object", "properties": { "origin_city": { "type": "string", "description": "The name of the city where the flight originates", }, "destination_city": { "type": "string", "description": "The flight destination city", }, }, "required": ["origin_city", "destination_city"], }, ) )
messages = [ SystemMessage(content="You an assistant that helps users find flight information."), UserMessage(content="I'm interested in going to Miami. What is the next flight there from Seattle?"), ]
# We expect the tool to be a function call ifisinstance(tool_call, ChatCompletionsToolCall):
# Parse the function call arguments and call the function function_args = json.loads(tool_call.function.arguments.replace("'", '"')) print(f"Calling function `{tool_call.function.name}` with arguments {function_args}") callable_func = locals()[tool_call.function.name] function_return = callable_func(**function_args) print(f"Function returned = {function_return}")
# Append the function call result fo the chat history messages.append(ToolMessage(tool_call_id=tool_call.id, content=function_return))
# Get another response from the model response = client.complete( messages=messages, tools=[flight_info], model=model_name, )