Created
September 30, 2024 11:53
-
-
Save Darkflib/766b593f81b009643ceae9402345f144 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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