File size: 2,215 Bytes
2bd30ee e893b95 bbd3773 2bd30ee 3ca229b 2bd30ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import os
import gradio as gr
from dotenv import load_dotenv
from langchain_community.chat_models import ChatOpenAI
from langchain_community.callbacks import get_openai_callback
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
from langchain.prompts import PromptTemplate
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from os import getenv
# Load environment variables
load_dotenv()
# Create the ChatOpenAI instance with the custom client
chat = ChatOpenAI(
openai_api_base="https://openrouter.ai/api/v1",
openai_api_key=getenv("OPENROUTER_API_KEY"),
model_name="nousresearch/hermes-3-llama-3.1-405b:free",
temperature=0.7,
)
# Set up the conversation memory
memory = ConversationBufferMemory(return_messages=True)
# Create a ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful AI assistant."),
MessagesPlaceholder(variable_name="history"),
("human", "{input}"),
]
)
# Create the conversation chain
conversation = ConversationChain(
llm=chat,
memory=memory,
prompt=prompt,
)
# Define the chat function
def chat_function(message, history):
with get_openai_callback() as cb:
response = conversation.predict(input=message)
token_count = cb.total_tokens
# Add token count to the response
response_with_tokens = f"{response}\n\n[Token count: {token_count}]"
return response_with_tokens
# Create the Gradio interface
iface = gr.ChatInterface(
chat_function,
chatbot=gr.Chatbot(height=400),
textbox=gr.Textbox(
placeholder="Type your message here...", container=False, scale=7
),
title="AI Chat with hermes-3-llama-3.1-405b",
description="Chat with the hermes-3-llama-3.1-405b model using OpenRouter and LangChain Community.",
theme="soft",
examples=[
"Tell me about yourself.",
"What's the weather like today?",
"Can you explain quantum computing?",
],
cache_examples=False,
retry_btn=None,
undo_btn="Delete Previous",
clear_btn="Clear",
)
# Launch the interface
if __name__ == "__main__":
iface.launch()
|