import requestsresponse = requests.post("http://localhost:11434/api/generate", json={"model": "llama3.1:8b", "prompt": "Hi, who are you?", "stream": False})print(response.content.decode())
{"model":"llama3.1:8b","created_at":"2024-10-15T07:29:47.257886Z","response":"I'm an artificial intelligence model known as Llama. Llama stands for \"Large Language Model Meta AI.\"","done":true,"done_reason":"stop","context":[128006,882,128007,271,13347,11,889,527,499,30,128009,128006,78191,128007,271,40,2846,459,21075,11478,1646,3967,439,445,81101,13,445,81101,13656,369,330,35353,11688,5008,16197,15592,1210],"total_duration":3170882792,"load_duration":46793334,"prompt_eval_count":16,"prompt_eval_duration":2294743000,"eval_count":23,"eval_duration":828374000}
这个示例中,Ollama 的 API 会在本地运行,并且你可以通过 HTTP 请求向它发送推理任务。
6. 查看运行状态
# 查看运行状态ollama ps# 停止模型ollama stop llama3.1:8b
16.1.2 Hugging Face 本地化部署
使用 Hugging Face 本地化部署 LLaMA 3 模型,可以通过以下步骤实现。假设你已经了解如何安装必要的软件环境和依赖包。
1. 确认系统配置
LLaMA 3 是一个大型模型,通常需要高性能的硬件,包括至少 24GB 显存的 GPU 和足够的 CPU 内存。
2. 安装依赖包
确保你安装了 Hugging Face Transformers 库和 Accelerate 库来进行模型的加载和加速:
pip install transformers accelerate
3. 获取 Hugging Face Access Token
由于 LLaMA 3 是 Meta 的受限模型,你需要访问 Hugging Face 上的相应页面,并通过请求访问权限。获取后,将 huggingface-cli 配置好你的令牌:
huggingface-cli login
4. 加载模型
一旦你有了访问权限,可以使用 Hugging Face 的 Transformers 库来加载模型并运行它。
Note: 在模型主页提交访问请求,等待批准。
from transformers import AutoTokenizer, AutoModelForCausalLMfrom IPython.display import Markdown# 加载 LLaMA 3 模型model_name ="meta-llama/Llama-3.2-1b"# 模型名称根据具体需求替换tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")# 推理inputs = tokenizer("hi, who are you?", return_tensors="pt").to("mps")outputs = model.generate(inputs.input_ids, max_length=50)print(tokenizer.decode(outputs[0], skip_special_tokens=True))
PT AU BA CA GP \
0 J Just, Berta Singla; Marks, Evan Alexander Neth... NaN NaN NaN
1 J Pereira-Marques, Joana; Ferreira, Rui M.; Figu... NaN NaN NaN
2 J Golzar-Ahmadi, Mehdi; Bahaloo-Horeh, Nazanin; ... NaN NaN NaN
3 J Li, Chenglong; Han, Yanfeng; Zou, Xiao; Zhang,... NaN NaN NaN
4 J Hu, Yu; Wang, Yulin; Wang, Runhua; Wang, Xiaok... NaN NaN NaN
TI \
0 Biofertilization increases soil organic carbon...
1 A metatranscriptomics strategy for efficient c...
2 Pathway to industrial application of heterotro...
3 A systematic discussion and comparison of the ...
4 Dirammox-dominated microbial community for bio...
SO VL IS BP ... \
0 INTERNATIONAL JOURNAL OF AGRICULTURAL SUSTAINA... 22 1 NaN ...
1 GUT MICROBES 16 1 NaN ...
2 BIOTECHNOLOGY ADVANCES 77 NaN NaN ...
3 SYNTHETIC AND SYSTEMS BIOTECHNOLOGY 9 4 775 ...
4 APPLIED MICROBIOLOGY AND BIOTECHNOLOGY 108 1 NaN ...
PY AB \
0 2024 Protecting and building soil carbon has become...
1 2024 The high background of host RNA poses a major ...
2 2024 The transition to renewable energies and elect...
3 2024 Synthetic microbial community has widely conce...
4 2024 Direct ammonia oxidation (Dirammox) might be o...
C1 CT CY SP CL TC \
0 [Just, Berta Singla; Llenas, Laia; Ponsa, Serg... NaN NaN NaN NaN 0
1 [Pereira-Marques, Joana; Ferreira, Rui M.; Fig... NaN NaN NaN NaN 2
2 [Golzar-Ahmadi, Mehdi; Norouzi, Forough; Holus... NaN NaN NaN NaN 0
3 [Li, Chenglong; Han, Yanfeng; Zou, Xiao; Zhang... NaN NaN NaN NaN 0
4 [Hu, Yu; Wang, Yulin; Liu, Shuang-Jiang] Shand... NaN NaN NaN NaN 0
WC UT
0 Agriculture, Multidisciplinary; Green & Sustai... WOS:001247571700001
1 Gastroenterology & Hepatology; Microbiology WOS:001176335800001
2 Biotechnology & Applied Microbiology WOS:001316252000001
3 Biotechnology & Applied Microbiology WOS:001273513000001
4 Biotechnology & Applied Microbiology WOS:001252334300001
[5 rows x 24 columns]
构建 JSON:每行数据提取出 UT、TI、AB 列的值,并构建一个包含 id、ti 和 ab 键的 JSON 对象。
json.dumps():将列表形式的 JSON 对象转换为 JSON 字符串。
ensure_ascii=False:确保输出的 JSON 字符串支持非 ASCII 字符。
import pandas as pdimport jsonfrom IPython.display import Markdowndef batch_to_json(data, batch_size):# 确保输入的 data 是一个 DataFrame,且 batch_size 是正整数ifnotisinstance(data, pd.DataFrame):raiseValueError("data 应该是一个 pandas DataFrame")ifnotisinstance(batch_size, int) or batch_size <=0:raiseValueError("batch_size 应该是一个正整数")# 提取 dataframe 中的每 batch_size 行数据 batches = [data[i:i+batch_size] for i inrange(0, len(data), batch_size)] result = []for batch in batches: json_batch = []for _, row in batch.iterrows():# 构建每行的 JSON 对象 json_obj = {"ID": row["UT"],"TI": row["TI"],"AB": row["AB"] } json_batch.append(json_obj) result.append(json.dumps(json_batch, ensure_ascii=False)) # 转换为 JSON 格式的字符串return result# 示例使用batch_size =2json_output = batch_to_json(data[0:1], batch_size)from pprint import pprintpprint(json_output)
['[{"ID": "WOS:001247571700001", "TI": "Biofertilization increases soil '
'organic carbon concentrations: results of a meta-analysis", "AB": '
'"Protecting and building soil carbon has become a global policy priority, '
'and novel agronomic fertilization practices may contribute to soil '
'protection and climate-smart agriculture. The application of microbial '
'inoculants (biofertilizers) is considered beneficial for soil and climate '
'-smart agriculture. Therefore, an exhaustive meta-analysis of '
'biofertilization studies was carried out worldwide to quantify the benefits '
'of microbial inoculants on SOC concentration. Based on 59 studies and 267 '
'observations, was found that biofertilizers significantly increased SOC '
'concentration by an average of 0.44 g C kg-1 soil. All biofertilizer types '
'were estimated to contribute positively to SOC (0.18-0.70 g C kg-1soil), but '
'only cyanobacteria, mixtures of organisms, mycorrhizal fungi, and nitrogen '
'fixers were statistically significant. In terms of crop type, results were '
'significant and positive for cereals, fruits, legumes and root/tuber crops '
'(0.44-0.82 g C kg-1soil). A significant positive linear relationship was '
'observed between crop yield and SOC changes, supporting the notion that '
'greater productivity helps explain SOC increases, accounting for 7% of the '
'dataset variability. This study provides the first evidence from a global '
'assessment that biofertilizer use is associated with an augmented '
'terrestrial agricultural organic carbon sink contributing to soil protection '
'and food security where climate-smart solutions are sought."}]']
16.2.3 调整提示词
Note: 将提示词换成英文效果可能会更好。
Note: JSON 格式要求键值对必须用双引号。
# 使用三引号插入一个非常长的系统提示符system_prompt =""" Please identify the relevance of each article's research topic to **synthetic microbial community** research based on the following content, and provide a score (0 for completely irrelevant, 10 for highly relevant, 1-9 for partially relevant):Please note:1. A synthetic microbial community refers to a composite of one or more microorganisms created by humans. It can be a probiotic, a biofertilizer, or a small microbial community. All research related to these topics should be considered relevant to synthetic microbial community research.2. The subsequent content will be provided in JSON format, including the article's ID, title (TI), and abstract (AB). Please make your judgment based on the title and abstract content, and record the ID for your response.3. Be sure to follow the response requirements below.Response requirements:1. In your response, please provide the article's ID, score, and a brief reason.2. Please use the JSON format for your response, for example:{"results": [{"ID":"string", "score":number, "reason": "string"}]}"""
[{'ID': 'WOS:001247571700001',
'reason': 'The article discusses the benefits of biofertilization on soil '
'organic carbon concentrations and its contribution to '
'climate-smart agriculture, which is relevant to synthetic '
'microbial community research as it involves a specific type of '
'microbe (microbial inoculants) used for agricultural purposes.',
'score': 9}]