LCELを使えばOK。
# %%
from dotenv import load_dotenv
import os
load_dotenv()
#print(os.environ['WATSONX_APIKEY'])
# %%
from langchain.globals import set_verbose
set_verbose(True)
# %%
from langchain_ibm.llms import WatsonxLLM
model_id = 'meta-llama/llama-3-70b-instruct'
project_id = '13facb99-f76c-4679-94c5-14e692b114db'
url = 'https://jp-tok.ml.cloud.ibm.com'
params = {
'decoding_method': 'sample',
'max_new_tokens': 4096,
'top_k': 50,
'top_p': 1,
'repetition_penalty': 1
}
llm = WatsonxLLM(
model_id=model_id,
project_id=project_id,
url=url,
params=params
)
# %%
from langchain_core.prompts.prompt import PromptTemplate
template = """
<|begin_of_text|>
<|start_header_id|>system<|end_header_id|>You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don’t know the answer to a question, please don’t share false information.<|eot_id|>
<|start_header_id|>user<|end_header_id|>
{history}
Human: {user}
<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
"""
prompt = PromptTemplate(template=template, input_variables=['history', 'user'])
# %%
from langchain.memory.buffer_window import ConversationBufferWindowMemory
memory = ConversationBufferWindowMemory()
# %%
from langchain_core.runnables.passthrough import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
chain = RunnablePassthrough().assign(
history = lambda x: memory.chat_memory
) | prompt | llm | StrOutputParser()
# %%
#user = 'watsonx.aiとは何ですか?'
#ai = ''
#for tmp in chain.stream({'user': user}):
# ai += tmp
# print(tmp)
#memory.chat_memory.add_user_message(user)
#memory.chat_memory.add_ai_message(ai)
#print(memory.chat_memory)
# %%
import gradio as gr
from llama3 import chain, memory
def fn(message, history):
user = message
ai = ''
for tmp in chain.stream({'user': user}):
ai += tmp
yield ai
memory.chat_memory.add_user_message(user)
memory.chat_memory.add_ai_message(ai)
#print(memory.chat_memory)
gr.ChatInterface(fn=fn).launch()