大模型-Tool call、检索增强

大模型

Tool call

心知天气:https://www.seniverse.com/

例子:调用天气接口 API

from openai import OpenAI
import requests
import json
"""
##### 天气接口 API 密钥获取:https://www.free-api.com/doc/558 #####
"""

# 定义调用天气 API 的函数
def get_weather(location, language="zh-Hans", unit="c"):
    url = "https://api.seniverse.com/v3/weather/now.json"
    params = {
        "key": "123",  # 替换为心知天气 Seniverse API Key
        "location": location,
        "language": language,
        "unit": unit,
    }
    response = requests.get(url, params=params)
    return response.json()


# # 初始化 OpenAI 客户端
# client = OpenAI(
#     api_key="",  # 替换为你的 OpenAI API Key
#     base_url="https://api.deepseek.com",  # DeepSeek 的 API 地址
# )

client = OpenAI(
    api_key="123", # 在这里将 MOONSHOT_API_KEY 替换为你从 Kimi 开放平台申请的 API Key
    base_url="https://api.moonshot.cn/v1",
)
model="moonshot-v1-8k"

# 发送消息并处理工具调用
def send_messages_with_tools(messages):
    response = client.chat.completions.create(
        model=model,  # 使用的模型
        messages=messages,
        tools=tools,
    )
    return response.choices[0].message

# 发送消息并处理工具调用
def send_messages_without_tools(messages):
    response = client.chat.completions.create(
        model=model,  # 使用的模型
        messages=messages,
    )
    return response.choices[0].message


# 定义工具(function calling)
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather for a specific location.",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city or region, e.g. Beijing",
                    },
                    "language": {
                        "type": "string",
                        "description": "The language of the response, e.g. zh-Hans",
                        "default": "zh-Hans",
                    },
                    "unit": {
                        "type": "string",
                        "description": "The unit of temperature, c for Celsius or f for Fahrenheit",
                        "default": "c",
                    },
                },
                "required": ["location"],  # location 是必填参数
            },
        },
    }
]

# 初始化对话
messages = [{"role": "user", "content": "上海现在的天气怎么样?"}]
print(f"User>\t {messages[0]['content']}")

# 发送消息并获取模型响应
message = send_messages_with_tools(messages)

# 检查是否有工具调用
if message.tool_calls:
    tool_call = message.tool_calls[0]
    print(f"Function Call>\t {tool_call}")
    messages.append(message)  # 将模型的消息添加到对话历史中

    # 调用天气 API 名字 参数 调用
    if tool_call.function.name == "get_weather":
        function_args = json.loads(tool_call.function.arguments) 
        weather_data = get_weather(**function_args)
        print(f"Weather Data>\t {weather_data}")

        # 将 API 返回的结果添加到对话历史中
        messages.append(
            {
                "role": "tool",
                "tool_call_id": tool_call.id,
                "content": str(weather_data),  # 将天气数据转换为字符串
            }
        )
        print(f"Messages>\t {messages}")

        # 再次发送消息,获取模型的最终响应
        final_response = send_messages_without_tools(messages)
        print(f"Model>\t {final_response.content}")

检索增强RAG

例子:分析文件内容并回答问题
代码:

import numpy as np
from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import Chroma
from transformers import pipeline

# Load documents 加载文档
loader = TextLoader("data/Jay_Chou.txt")
documentation = loader.load()
 
# Split documents into chunks 将文档分割成块
text_splitter = CharacterTextSplitter(chunk_size=128, chunk_overlap=0)
documentation = text_splitter.split_documents(documentation)

# Define embedding model 定义嵌入模型
model_name = "sentence-transformers/all-MiniLM-L6-v2"
# model_name = "data/models--sentence-transformers--all-MiniLM-L6-v2"
model_kwargs = {'device': 'cpu'}
encode_kwargs = {'normalize_embeddings': True}
embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs)

# Create vector store 创建向量存储
db = Chroma.from_documents(documentation, embeddings)

# Perform similarity search 执行相似性搜索
query = "Who is Jay Chou's wife?" # 周杰伦的妻子是谁
results = db.similarity_search(query)

# Print retrieved document content 打印检索到的文档内容
print("Retrieved document content:", results[0].page_content)

# Load Hugging Face pipeline for question answering 加载Hugging Face问答管道
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad", tokenizer="distilbert-base-uncased-distilled-squad")

# Manually format the context for the pipeline 手动格式化管道的上下文
context = results[0].page_content  # Access the content of the most relevant document 访问最相关文档的内容
input_data = {
    "context": context,
    "question": query
}

# Print input to the model 将输入打印到模型
print("Input to the model:", input_data)

# Get the answer using Hugging Face pipeline 使用Hugging Face管道获取答案
answer = qa_pipeline(input_data)

# Print the result 打印结果
print("Answer from the model模型的答案:", answer['answer'])

分析数据:Jay_Chou.txt

Jay Chou, born on January 18, 1979, in New Taipei City, Taiwan, is a renowned singer, songwriter, music producer, director, and actor from Taiwan. He is one of the most influential figures in Chinese pop music and is often referred to as the "King of Chinese Pop" or a representative figure of the genre.

Musical Achievements:
Jay Chou's music style is unique, blending various elements such as pop, R&B, rock, hip hop, and electronic music, while emphasizing poetic and profound lyrics. His works not only break the boundaries of traditional Chinese pop music but also skillfully integrate Western musical elements, creating a new trend in music.

In 2000, Jay Chou released his debut album Jay, which instantly made him famous and earned him numerous music awards. Since then, every album he has released has sparked great responses and become an important milestone in Chinese pop music. His representative songs include Blue and White Porcelain, Simple Love, Rice Fragrance, East Wind Breaks, Listen to Mama, and others.

Songwriting Style:
Jay Chou's songs have a modern feel while often incorporating elements of traditional Chinese culture, especially in his ancient-style songs such as East Wind Breaks and Chrysanthemum Terrace, which reflect his unique understanding and inheritance of Chinese culture. Additionally, his lyrics are often creative and full of personality, sometimes carrying humor or philosophical meanings, and his linguistic style is distinctive, often praised by his fans.

Film Career:
In addition to music, Jay Chou has also ventured into the film industry, taking on roles as a director, screenwriter, and actor. His self-written, directed, and starred film Secret (2007) achieved great success, showcasing not only his musical talents but also his potential in filmmaking. He has also appeared in several films, such as Initial D and Kung Fu Dunk, further expanding his influence in the entertainment industry.

Personal Life:
Jay Chou leads a low-key private life. In 2005, he married his wife, Hannah Quinlivan, and they have a son and a daughter. Hannah is both his wife and a partner in his career, and the two share a deep relationship, often posting their life moments on social media.

Influence and Honors:
Jay Chou is regarded as one of the innovators of Chinese pop music, and his works have a widespread influence among Chinese-speaking communities worldwide. He has won countless music awards and successfully brought Chinese pop music to international stages. His musical style and influence transcend different eras and regions, making him beloved by audiences of all ages.

In summary, Jay Chou is not only a superstar in the Chinese music scene but also a symbol of Chinese pop culture due to his innovative spirit and musical talent.

导入各种包:pip install XXX
如果无法连接到https://huggingface.co/,需要设置国内镜像:

set HF_ENDPOINT=https://hf-mirror.com

需要在要运行的代码目录下,cmd执行该命令
参考文档:https://hf-mirror.com/

你可能感兴趣的:(ai大模型,python,数据库,conda,ai)