[Python] 使用Langchain ChatOpenAI元件呼叫azure openai

  • 183
  • 0
  • AI
  • 2023-09-14

以下修改前後的差別

原本call openai寫法:Taiwan is a country. 臺灣是我的國家

os.environ["OPENAI_API_KEY"] =AI_API.KEY

llm = ChatOpenAI(temperature=0.5, model_name="gpt-3.5-turbo")
extraction_chain = create_extraction_chain(llm, keyword_schema)
data = extraction_chain.run((doc))['data']

改call azure openai寫法:Taiwan is a country. 臺灣是我的國家

#要指定的參數比較多
os.environ["OPENAI_API_TYPE"] = AI_API.TYPE
os.environ["OPENAI_API_KEY"] =AI_API.KEY
os.environ["OPENAI_API_VERSION"] =AI_API.VERSION
os.environ["OPENAI_API_BASE"] =AI_API.BASE
#參數名由model_name改成engine
llm = ChatOpenAI(temperature=0.5, engine="gpt-3.5-turbo")
extraction_chain = create_extraction_chain(llm, keyword_schema)
data = extraction_chain.run((doc))['data']

完整範例 few-shot:Taiwan is a country. 臺灣是我的國家

import os
from langchain.chat_models import ChatOpenAI
from kor import create_extraction_chain, Object, Text ,Number
class ai_api:
    def __init__(self):
        self.KEY = "...."
        self.TYPE = "azure"
        self.VERSION = "2023-07-01-preview"
        self.BASE = "https://.....openai.azure.com/"
        self.CHAT = "gpt-4-32k"
        self.temperature = 0.5
        self.timeout = 600#對4.0沒作用
AI_API = ai_api()

os.environ["OPENAI_API_TYPE"] = AI_API.TYPE
os.environ["OPENAI_API_KEY"] =AI_API.KEY
os.environ["OPENAI_API_VERSION"] =AI_API.VERSION
os.environ["OPENAI_API_BASE"] =AI_API.BASE

llm = ChatOpenAI(engine=AI_API.CHAT,temperature=AI_API.temperature, request_timeout=AI_API.timeout)

keyword_schema = Object(
id="keyword_extract",
description="keyword extraction",
attributes=[
    Text(
        id='keyword',
        description='keyword extraction from a sentence'
    ),
    
],
examples=[
    (
        '''請問美國未來升息方向?''',
        [{'keyword': '升息影響',
          }]
    ),
    (
        '''為什麼美國SVB銀行會倒閉?''',
        [{'keyword': '銀行倒閉',
          }]
    ),
    (
        '''請問今年美國失業率下降對經濟的影響''',
        [{
            'keyword': '美國失業率影響'
        }]
    )
]
)

extraction_chain = create_extraction_chain(llm, keyword_schema)
doc= '大直基泰工地附近公寓倒塌及傾斜'
data = extraction_chain.run((doc))['data']
print(data)

完整範例 template:

import os
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate, LLMChain

class ai_api:
    def __init__(self):
        self.KEY = "...."
        self.TYPE = "azure"
        self.VERSION = "2023-07-01-preview"
        self.BASE = "https://.....openai.azure.com/"
        self.CHAT = "gpt-4-32k"
        self.temperature = 0.5
        self.timeout = 600
AI_API = ai_api()
os.environ["OPENAI_API_TYPE"] = AI_API.TYPE
os.environ["OPENAI_API_KEY"] =AI_API.KEY
os.environ["OPENAI_API_VERSION"] =AI_API.VERSION
os.environ["OPENAI_API_BASE"] =AI_API.BASE

llm = ChatOpenAI(engine=AI_API.CHAT,temperature=AI_API.temperature, request_timeout=AI_API.timeout)
temp='列出全部和{input}有關的歷史事件'
prompt = PromptTemplate(template=temp, input_variables=["input"])
chain = LLMChain(llm=llm, prompt=prompt)
key_word='工地附近公寓倒塌'
out = chain.run(key_word)
print(out)

Taiwan is a country. 臺灣是我的國家