Skip to content

Instantly share code, notes, and snippets.

@csiebler
Last active July 25, 2023 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csiebler/303ae7cf821a90abfebc12c73f4f6835 to your computer and use it in GitHub Desktop.
Save csiebler/303ae7cf821a90abfebc12c73f4f6835 to your computer and use it in GitHub Desktop.
Short snippet on how to use gpt-35-turbo (ChatGPT model) with LangChain in Azure OpenAI Service
# Make sure you have the latest openai and langchain versions running:
# pip install openai --upgrade
# pip install langchain --upgrade
# This code was tested with openai==0.27.8 and langchain==0.0.240
import os
import openai
from dotenv import load_dotenv
from langchain.chat_models import AzureChatOpenAI
from langchain import LLMChain
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
# Load environment variables (set OPENAI_API_KEY, OPENAI_API_BASE, and OPENAI_API_VERSION in .env)
load_dotenv()
# Configure Azure OpenAI Service API
openai.api_type = "azure"
openai.api_base = os.getenv('OPENAI_API_BASE')
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_version = os.getenv('OPENAI_API_VERSION')
# Init LLM and embeddings model
llm = AzureChatOpenAI(deployment_name="gpt-35-turbo", temperature=0.7)
system_message = "You are an AI assistant that tells jokes."
system_message_prompt = SystemMessagePromptTemplate.from_template(system_message)
human_template="{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
chain = LLMChain(llm=llm, prompt=chat_prompt)
result = chain.run(f"Tell me a dad joke")
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment