台北來的土包子
 
Chatbot -Python and Azure OpenAI

Chatbot -Python and Azure OpenAI

import os
import openai
openai.api_type  = "azure"
openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT")     # Endpoint URL
openai.api_version = "2023-05-15"
openai.api_key =  os.getenv("AZURE_OPENAI_KEY")          # API KEY

while True:
    question= input("Your question (quit to exit this chat):")

    if question == "quit":
        print("Bye")
        break

    response = openai.ChatCompletion.create(
        engine=os.getenv("AZURE_OPENAI_ENGINE"),          #  Deployment name on Azure AI Studio   
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": question},
        ]
    )

    print(response['choices'][0]['message']['content'])
  • 要使用 Azure Open AI 必需先透過表單申請經核准才能使用 (2023-10-06)
  • engine 的部份,需在 Azure AI Studio 上部署模型後才能使用,所需的程式碼在 playground 上可以看的到 (view code)
  • 使用 os.getenv 方法,需先安裝 python-dotenv, 並在 .env 檔裡設定相關的變數

讓 Chatbot 用 python 寫一段溫度轉換的結果:

Your question (quit to exit this chat): give me a python code that can do temperature conversion
Certainly! Here is a Python code that performs temperature conversion between Celsius, Fahrenheit, and Kelvin:

```python
def celsius_to_fahrenheit(celsius):
    fahrenheit = (celsius * 9/5) + 32
    return fahrenheit

def fahrenheit_to_celsius(fahrenheit):
    celsius = (fahrenheit - 32) * 5/9
    return celsius

def celsius_to_kelvin(celsius):
    kelvin = celsius + 273.15
    return kelvin

def kelvin_to_celsius(kelvin):
    celsius = kelvin - 273.15
    return celsius

def fahrenheit_to_kelvin(fahrenheit):
    kelvin = (fahrenheit + 459.67) * 5/9
    return kelvin

def kelvin_to_fahrenheit(kelvin):
    fahrenheit = (kelvin * 9/5) - 459.67
    return fahrenheit

# Conversion examples

# Convert 25 degrees Celsius to Fahrenheit
celsius = 25
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius} degrees Celsius is equal to {fahrenheit} degrees Fahrenheit.")

# Convert 100 degrees Fahrenheit to Celsius
fahrenheit = 100
celsius = fahrenheit_to_celsius(fahrenheit)
print(f"{fahrenheit} degrees Fahrenheit is equal to {celsius} degrees Celsius.")

# Convert 0 degrees Celsius to Kelvin
celsius = 0
kelvin = celsius_to_kelvin(celsius)
print(f"{celsius} degrees Celsius is equal to {kelvin} Kelvin.")

# Convert 273.15 Kelvin to Celsius
kelvin = 273.15
celsius = kelvin_to_celsius(kelvin)
print(f"{kelvin} Kelvin is equal to {celsius} degrees Celsius.")

# Convert 50 degrees Fahrenheit to Kelvin
fahrenheit = 50
kelvin = fahrenheit_to_kelvin(fahrenheit)
print(f"{fahrenheit} degrees Fahrenheit is equal to {kelvin} Kelvin.")

# Convert 500 Kelvin to Fahrenheit
kelvin = 500
fahrenheit = kelvin_to_fahrenheit(kelvin)
print(f"{kelvin} Kelvin is equal to {fahrenheit} degrees Fahrenheit.")
```

This code defines six conversion functions for converting between Celsius, Fahrenheit, and Kelvin. These functions take the input temperature and perform the necessary calculations to convert it to the desired temperature scale.

You can uncomment the conversion examples at the end of the code to see the conversion results for specific temperature values.
Your question (quit to exit this chat):quit
Bye

發表迴響