Skip to content

Commit

Permalink
大模型
Browse files Browse the repository at this point in the history
  • Loading branch information
dlimeng committed Mar 29, 2024
1 parent 04dd5b1 commit fc9bd43
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Llama/LlamaDemo3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os
from transformers import AutoTokenizer, AutoModelForCausalLM
from sentence_transformers import SentenceTransformer, util
import torch

# 加载预训练模型的分词器和模型
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-chat-hf",
device_map='auto')

# 加载HuggingFace的Embeddings模型
embedder = SentenceTransformer('all-MiniLM-L6-v2')

# 指定知识库文件目录
knowledge_base_dir = "path/to/your/knowledge/base"

# 读取目录中的所有文件,并将其内容作为知识库条目
knowledge_base = []
for filename in os.listdir(knowledge_base_dir):
filepath = os.path.join(knowledge_base_dir, filename)
if os.path.isfile(filepath):
with open(filepath, 'r', encoding='utf-8') as file:
knowledge_base.append(file.read())

# 将知识库文本转换为embeddings
knowledge_embeddings = embedder.encode(knowledge_base, convert_to_tensor=True)

# 定义一个用户的问题或提示
prompt = "请给我讲个玫瑰的爱情故事?"

# 将用户的问题转换为embedding
question_embedding = embedder.encode(prompt, convert_to_tensor=True)

# 在知识库中寻找最相似的文本
cos_scores = util.pytorch_cos_sim(question_embedding, knowledge_embeddings)[0]
top_result = torch.topk(cos_scores, k=1)

# 使用找到的最相关的文本作为模型的输入
input_text = knowledge_base[top_result.indices[0]]

# 使用分词器将找到的最相关的文本转化为模型可以理解的格式
inputs = tokenizer(input_text, return_tensors="pt").to("cuda")

# 使用模型生成文本
outputs = model.generate(inputs["input_ids"], max_new_tokens=2000)

# 将生成的令牌解码成文本
response = tokenizer.decode(outputs[0], skip_special_tokens=True)

# 打印生成的响应
print(response)
62 changes: 62 additions & 0 deletions Llama/LlamaDemo4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from openai import OpenAI
from typing_extensions import override
from openai import AssistantEventHandler


client = OpenAI(api_key="")


assistant = client.beta.assistants.create(
name="Math Tutor",
instructions="You are a personal math tutor. Write and run code to answer math questions.",
tools=[{"type": "code_interpreter"}],
model="gpt-4-turbo-preview",
)

thread = client.beta.threads.create()

message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
)



# First, we create a EventHandler class to define
# how we want to handle the events in the response stream.

class EventHandler(AssistantEventHandler):
@override
def on_text_created(self, text) -> None:
print(f"\nassistant > ", end="", flush=True)

@override
def on_text_delta(self, delta, snapshot):
print(delta.value, end="", flush=True)

def on_tool_call_created(self, tool_call):
print(f"\nassistant > {tool_call.type}\n", flush=True)

def on_tool_call_delta(self, delta, snapshot):
if delta.type == 'code_interpreter':
if delta.code_interpreter.input:
print(delta.code_interpreter.input, end="", flush=True)
if delta.code_interpreter.outputs:
print(f"\n\noutput >", flush=True)
for output in delta.code_interpreter.outputs:
if output.type == "logs":
print(f"\n{output.logs}", flush=True)


# Then, we use the `create_and_stream` SDK helper
# with the `EventHandler` class to create the Run
# and stream the response.

with client.beta.threads.runs.create_and_stream(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as Jane Doe. The user has a premium account.",
event_handler=EventHandler(),
) as stream:
stream.until_done()
29 changes: 29 additions & 0 deletions ai-generated-daily/2024-03-11.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
1.prompt策略遇上分治算法

论文地址:https://arxiv.org/pdf/2402.05359.pdf

2.不依赖token,字节级模型,直接处理二进制

论文:https://arxiv.org/abs/2402.19155

代码:https://github.com/sanderwood/bgpt

模型:https://huggingface.co/sander-wood/bgpt

项目主页:https://byte-gpt.github.io

3.马斯克宣布本周开源Grok

https://x.com/elonmusk/status/1767108624038449405?s=20

4.RAG搜索复杂PDF文档

https://x.com/llama_index/status/1766862130761125958?s=20

5.几行代码微调LLM Agent

https://x.com/khoomeik/status/1766805213644800011?s=20

6.使用LoRA技术微调Gemma-2B模型

https://huggingface.co/blog/gemma-peft
Empty file.
36 changes: 36 additions & 0 deletions ollama/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from vanna.ollama import Ollama
from vanna.chromadb.chromadb_vector import ChromaDB_VectorStore

class MyVanna(ChromaDB_VectorStore, Ollama):
def __init__(self, config=None):
ChromaDB_VectorStore.__init__(self, config=config)
Ollama.__init__(self, config=config)


vn = MyVanna(config={'model': 'gemma:7b'})

vn.connect_to_sqlite('my-database.sqlite')

df_ddl = vn.run_sql("SELECT type, sql FROM sqlite_master WHERE sql is not null")

for ddl in df_ddl['sql'].to_list():
vn.train(ddl=ddl)

vn.train(ddl="""
CREATE TABLE IF NOT EXISTS my-table (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT
)
""")

vn.train(
documentation="Our business defines OTIF score as the percentage of orders that are delivered on time and in full")

vn.train(sql="SELECT * FROM my-table WHERE name = 'John Doe'")

training_data = vn.get_training_data()

from vanna.flask import VannaFlaskApp

VannaFlaskApp(vn).run()
2 changes: 2 additions & 0 deletions ollama/demo2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import vanna
print(dir(vanna))
41 changes: 41 additions & 0 deletions openai/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from openai import OpenAI

client = OpenAI(
# defaults to os.environ.get("OPENAI_API_KEY")
api_key="",
base_url="https://api.chatanywhere.tech/v1"
)



# 非流式响应
def gpt_35_api(messages: list):
"""为提供的对话消息创建新的回答
Args:
messages (list): 完整的对话消息
"""
completion = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages)
print(completion.choices[0].message.content)

def gpt_35_api_stream(messages: list):
"""为提供的对话消息创建新的回答 (流式传输)
Args:
messages (list): 完整的对话消息
"""
stream = client.chat.completions.create(
model='gpt-3.5-turbo',
messages=messages,
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")

if __name__ == '__main__':
messages = [{'role': 'user','content': '鲁迅和周树人的关系'},]
# 非流式调用
# gpt_35_api(messages)
# 流式调用
gpt_35_api_stream(messages)

0 comments on commit fc9bd43

Please sign in to comment.