众星捧月算法比赛-知识工程的简单实现
赛题介绍
参考比赛官网:https://be-loved.cn/zte-student-match/main/zsgc
https://zhuanlan.zhihu.com/p/683323150
一、LLM
Qwen1.5-4B-Chat
二、代码
代码如下(示例):参考链接 https://zhuanlan.zhihu.com/p/683323150
import logging
import sys,os
from IPython.display import Markdown, display
import torch
from llama_index.llms.huggingface import HuggingFaceLLM
from llama_index.core.prompts import PromptTemplate
from modelscope import snapshot_download
from llama_index.core.base.embeddings.base import BaseEmbedding, Embedding
from abc import ABC
from typing import Any, List, Optional, Dict, cast
from llama_index.core import (
VectorStoreIndex,
ServiceContext,
set_global_service_context,
SimpleDirectoryReader,
)
os.environ['HF_ENDPOINT']='https://hf-mirror.com'
# Model names
qwen2_4B_CHAT = "qwen/Qwen1.5-4B-Chat"
selected_model = snapshot_download(qwen2_4B_CHAT)
SYSTEM_PROMPT = """
You are participating in a technical salon hosted by ZTE Magazine.
The event organizer has prepared a knowledge quiz to increase interaction and fun in academic exchanges.
The quiz contains descriptions of cutting-edge achievements from various academic journals published by ZTE Magazine.
Some descriptions may contain hidden errors, so if you find that an answer is missing or unclear, please respond with 'NA' for Not Available.
Your task is to identify whether the given statements are true or false.
Respond with 'T' for True, 'F' for False, and 'NA' for Not Available.
"""
query_wrapper_prompt = PromptTemplate(
"[INST]<<SYS>>\n" + SYSTEM_PROMPT + "<</SYS>>\n\n{query_str}[/INST] "
)
llm = HuggingFaceLLM(
context_window=4096,
max_new_tokens=2048,
generate_kwargs={"temperature": 0.0, "do_sample": False},
query_wrapper_prompt=query_wrapper_prompt,
tokenizer_name=selected_model,
model_name=selected_model,
device_map="auto",
# change these settings below depending on your GPU
model_kwargs={"torch_dtype": torch.float16},
)
documents = SimpleDirectoryReader('databases/process/reference').load_data()
embedding_model = "iic/nlp_gte_sentence-embedding_chinese-base"
class ModelScopeEmbeddings4LlamaIndex(BaseEmbedding, ABC):
embed: Any = None
model_id: str = "iic/nlp_gte_sentence-embedding_chinese-base"
def __init__(
self,
model_id: str,
**kwargs: Any,
) -> None:
super().__init__(**kwargs)
try:
from modelscope.models import Model
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
# 使用modelscope的embedding模型(包含下载)
self.embed = pipeline(Tasks.sentence_embedding, model=self.model_id)
except ImportError as e:
raise ValueError(
"Could not import some python packages." "Please install it with `pip install modelscope`."
) from e
def _get_query_embedding(self, query: str) -> List[float]:
text = query.replace("\n", " ")
inputs = {"source_sentence": [text]}
return self.embed(input=inputs)['text_embedding'][0].tolist()
def _get_text_embedding(self, text: str) -> List[float]:
text = text.replace("\n", " ")
inputs = {"source_sentence": [text]}
return self.embed(input=inputs)['text_embedding'][0].tolist()
def _get_text_embeddings(self, texts: List[str]) -> List[List[float]]:
texts = list(map(lambda x: x.replace("\n", " "), texts))
inputs = {"source_sentence": texts}
return self.embed(input=inputs)['text_embedding'].tolist()
async def _aget_query_embedding(self, query: str) -> List[float]:
return self._get_query_embedding(query)
embeddings = ModelScopeEmbeddings4LlamaIndex(model_id=embedding_model)
service_context = ServiceContext.from_defaults(embed_model=embeddings, llm=llm)
set_global_service_context(service_context)
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("中兴是一家通讯公司")
print(response)
import pandas as pd
question_path='databases/process/test_A.csv'
def process_question(quession):
response = query_engine.query(quession)
return response
import time
df['answer'] = df['question'].apply(process_question)
output_csv_path = 'result.csv'
df.drop(columns=['question'],inplace=True)
df.to_csv(output_csv_path, index=False)
总结
通过HuggingFaceLLM 和llamaindex实现了LLM+RAG的基本流程,但是模型的算法结果并不好,需要调优处理。