from openai import AzureOpenAIclient = AzureOpenAI( azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), api_key=os.getenv("AZURE_OPENAI_API_KEY"), api_version="2024-05-01-preview" )normal_chain = ( ChatPromptTemplate.from_messages([("system", "write a tweet about {topic} in the style of Elon Musk") ]) | client | StrOutputParser())
在这段代码中,我遇到了类型错误,期望类型是 ‘Runnable[Any, Other] | (Any) -> Other | (Iterator) -> Iterator[Other] | Mapping[str, Runnable[Any, Other] | (Any) -> Other | Any]’,但实际得到的是 ‘AzureOpenAI’。如何将客户端设为Runnable?有没有其他方法可以实现同样的事情?
我尝试了LangChain提供的所有可用选项,但都没有成功。
回答:
client = AzureOpenAI( azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), api_key=os.getenv("AZURE_OPENAI_API_KEY"), api_version="2024-05-01-preview" ) completion = client.chat.completions.create( model=finetuned_deployment, messages=[ { "role": "user", "content": f"Write a tweet about {topic}" }], max_tokens=800, temperature=0.7, top_p=0.95, ) completion.to_json()
这段代码片段可以正常工作。