0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

watsonx.ai/mistral large2で同義語を生成

Last updated at Posted at 2024-11-14
test.py
# %%
import sys
from langchain_ibm.llms import WatsonxLLM
from langchain_core.prompts.prompt import PromptTemplate
from langchain.chains.llm import LLMChain

# %%
keyword = sys.argv[1]

# %%
model_id = 'mistralai/mistral-large'
project_id = '<PROJECT_ID>'
url = 'https://us-south.ml.cloud.ibm.com/'
apikey = '<APIKEY>'
params = {
    'decoding_method': 'greedy',
    'max_new_tokens': 16384,
    'min_new_tokens': 1,
    'repetition_penalty': 1.02,
    'stop_sequences': ['</s>']
}
llm = WatsonxLLM(
    model_id=model_id,
    project_id=project_id,
    url=url,
    apikey=apikey,
    params=params
)

# %%
template = """<s>[INST]
### 指示:
あなたは物質材料の専門家です。
質問に対して、形式に従って回答して下さい。

### 形式:
{keyword},類義語1,同義語2,同義語3,同義語4,同義語5,同義語6,同義語7,同義語8,同義語9,同義語10

### 質問:
{keyword}に関する英語の同義語を最大10個まで出力して下さい。

[/INST]
{keyword},"""

# %%
prompt = PromptTemplate(template=template, input_variables=['keyword'])
chain = LLMChain(llm=llm, prompt=prompt)

# %%
response = chain.invoke(input={'keyword': keyword})
answer = response['text']
print(keyword, answer, sep=',')

# %%
$ python test.py アルミニウム
ルミニウム,aluminum,aluminium,Al,aluminous,aluminiferous,aluminic,aluminous
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?