Context之 大模型sampling开发

工具可以通过采样(生成文本)与大型语言模型(LLMs)进行交互。

采样sampling演示:mcp服务端通过上下文来回调客户端的大模型,得到结果。

服务端代码:

"""6.5 【上下文类】Context 大模型sampling服务端开发"""
from mcp import SamplingMessage
from mcp.server.fastmcp import FastMCP,Context
from mcp.types import TextContent

#创建mcp实例
mcp = FastMCP("Context with sampling MCP Server")

@mcp.tool()
async def sampling_tool(ctx:Context):
    """
    大模型sampling服务端开发
    :param ctx: 上下文类
    :return: 处理完成
    """
    response = await ctx.session.create_message(
        messages=[
          SamplingMessage(
              role="user",
              content = TextContent(
                  text="帮我编写一个小学放假通知的文章,字数在100-200字之间。",
                  type="text",
              )
          )
        ],max_tokens=1024
    )
    #打印response
    print("response:",response)
    return "大模型sampling回调完成"



if __name__ == '__main__':
    print("MCP Server is running...")
    mcp.run(transport='sse')

客户端代码:

import asyncio
import json
from contextlib import AsyncExitStack
from typing import Optional
from mcp import ClientSession
from mcp.server.fastmcp import Context
from mcp.types import CreateMessageRequestParams, CreateMessageResult, TextContent
f
### 主流的AI大模型应用开发框架 #### 一、VLLM VLLM 是一个高性能的语言模型推理和部署库,支持多种大规模预训练语言模型。该框架优化了多GPU环境下的性能表现,在处理超大型数据集时展现出显著优势[^3]。 ```python from vllm import LLM, SamplingParams # 初始化模型实例 model = LLM(model="path/to/model") # 设置采样参数 params = SamplingParams(temperature=0.8) # 执行预测任务 output = model.generate(prompt="Once upon a time", sampling_params=params) print(output) ``` #### 二、OLLAMA OLLAMA 提供了一个简单易用的API接口来加载不同的大模型,并允许用户自定义配置文件以适应特定应用场景的需求。它还具备良好的扩展性和灵活性,能够方便地与其他工具集成。 ```python import ollama # 加载指定名称的大规模预训练模型 model = ollama.load_model('my_large_language_model') # 使用默认设置生成文本 text = model.predict("The capital of France is") print(text) ``` #### 三、昇腾框架(Ascend) 由华为研发的昇腾系列处理器配套使用的软件栈之一——昇腾框架,则专为加速AI计算而设计。其不仅兼容PyTorch、TensorFlow等多个流行机器学习平台,而且针对NPU进行了特别优化,从而实现了更高效的资源利用率与更快的速度提升。 ```bash pip install mindspore # 安装MindSpore作为昇腾框架的一部分 # Python代码示例 from mindspore import context, nn context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") net = nn.Dense(in_channels=16, out_channels=10) loss_fn = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean') opt = nn.Momentum(params=net.trainable_params(), learning_rate=0.01, momentum=0.9) with SummaryRecord(log_dir="./summary"): ... ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值