watsonx.aiのLlama 3でチャットボットを適当に開発＆適当に会話してみた

関数作って、、、
```python: llama3.py
# %%
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 = PROJECT_ID
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
from langchain.chains.llm import LLMChain
from langchain.memory.buffer_window import ConversationBufferWindowMemory

template = """



system
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.


user

{history}
Human: {user}



assistant

"""
prompt = PromptTemplate(template=template, input_variables=['history', 'user'])
memory = ConversationBufferWindowMemory()
chain = LLMChain(llm=llm, prompt=prompt, memory=memory)
# %%
def chatbot(user):
    return chain.invoke(input={'history': memory.chat_memory, 'user': user})
```
UI作って、、、
```python: app.py
import gradio as gr
from llama3 import chatbot

def random_response(message, history):
    return chatbot(user=message)['text']

gr.ChatInterface(random_response).launch()
```
起動。
```bash
python app.py
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/961b6910-4b27-3f91-858a-7dd862832f26.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/d3ae51b7-ccaf-4994-4fb9-aa32d0c85dec.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/b8b026a0-203a-d248-9f7c-430b3c98b924.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/24de40a8-9588-a64c-01a7-18ff0d87e0f0.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/ac9429dd-a5b3-ac1a-546b-23006fa8ea33.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/2dfa7100-4c86-b006-5356-dc19db559fb9.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/77409dc5-2388-f5e4-9849-273c228a376b.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/b9e52995-be3d-d033-2052-6bce1161482a.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/78640303-d8d2-dd41-0e87-1ee0b2c665b2.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/2b9661d1-d322-0830-b7f9-1313ba0002ab.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/ff588ca0-bccd-62e6-9e8a-b5899a0761a0.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/eeab3693-392a-f2cb-dcc2-ede8e66ecfc0.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/720728cb-23ef-4900-7c7c-8f4e9f67582c.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3756172/ef68ad74-b819-6ae3-930b-b669b3b0dd1b.png)