Skip to content

Instantly share code, notes, and snippets.

@Darkflib
Created September 30, 2024 11:53
Show Gist options
  • Save Darkflib/766b593f81b009643ceae9402345f144 to your computer and use it in GitHub Desktop.
Save Darkflib/766b593f81b009643ceae9402345f144 to your computer and use it in GitHub Desktop.
from langchain.llms import OpenAI
from langchain.callbacks.base import BaseCallbackHandler
# Example Callback Handler
class CustomCallbackHandler(BaseCallbackHandler):
def on_llm_start(self, serialized, prompts, **kwargs):
print("LLM is starting...")
print("Prompts:", prompts)
def on_llm_new_token(self, token, **kwargs):
print(f"Received token: {token}")
def on_llm_end(self, response, **kwargs):
print("LLM call finished!")
print("Response:", response)
# Initialize the OpenAI LLM with the callback handler
llm = OpenAI(openai_api_key='your-api-key', callbacks=[CustomCallbackHandler()], streaming=True)
# Make a call to the model
response = llm("Explain the difference between AI and Machine Learning.")
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment