影刀:通过代码来调用api模型

今天给大家分享的是通过影刀代码模块来调用api模型

首先需要准备好api网站,这里我推荐的是云雾api,里面各种模型基本上都有

https://api.wlai.vip/about

进入云雾以后,先进行注册,然后获取你的令牌

准备工作做完以后,我们需要来写代码了,代码部分我们可以参考云雾官方文档里面的代码,这个地方有很多的模型代码可供参考

我们以谷歌模型为例,点击右边的生成代码既可获取到想要的代码,代码内容如下

from typing import Optional, Any, TypeVar, Type, cast


T = TypeVar("T")


def from_str(x: Any) -> str:
    assert isinstance(x, str)
    return x


def from_none(x: Any) -> Any:
    assert x is None
    return x


def from_union(fs, x):
    for f in fs:
        try:
            return f(x)
        except:
            pass
    assert False


def to_class(c: Type[T], x: Any) -> dict:
    assert isinstance(x, c)
    return cast(Any, x).to_dict()


class ApifoxModel:
    accept: str
    authorization: Optional[str]
    content_type: str

    def __init__(self, accept: str, authorization: Optional[str], content_type: str) -> None:
        self.accept = accept
        self.authorization = authorization
        self.content_type = content_type

    @staticmethod
    def from_dict(obj: Any) -> 'ApifoxModel':
        assert isinstance(obj, dict)
        accept = from_str(obj.get("Accept"))
        authorization = from_union([from_str, from_none], obj.get("Authorization"))
        content_type = from_str(obj.get("Content-Type"))
        return ApifoxModel(accept, authorization, content_type)

    def to_dict(self) -> dict:
        result: dict = {}
        result["Accept"] = from_str(self.accept)
        if self.authorization is not None:
            result["Authorization"] = from_union([from_str, from_none], self.authorization)
        result["Content-Type"] = from_str(self.content_type)
        return result


def apifox_model_from_dict(s: Any) -> ApifoxModel:
    return ApifoxModel.from_dict(s)


def apifox_model_to_dict(x: ApifoxModel) -> Any:
    return to_class(ApifoxModel, x)

在影刀中写入上面的代码,然后再调用下面这串代码,令牌填入其中,即可调用成功

# 使用提醒:
# 1. xbot包提供软件自动化、数据表格、Excel、日志、AI等功能
# 2. package包提供访问当前应用数据的功能,如获取元素、访问全局变量、获取资源文件等功能
# 3. 当此模块作为流程独立运行时执行main函数
# 4. 可视化流程中可以通过"调用模块"的指令使用此模块

import xbot
from xbot import print, sleep
from .import apifox_model
from .apifox_model import ApifoxModel, Message
import requests

def main(args):
    pass

def dialogue_with_model(first_question, second_question):
    # 定义固定的参数
    model_id = "gemini-1.5-flash-latest"
    api_url = "https://yunwu.ai/v1/chat/completions"  # 确保URL正确,移除HTML实体
    api_key = "1111111111111111111"# 填入你的令牌
    first_round_temp = 0.8
    first_round_top_p = 0.7
    first_round_presence_penalty = 0
    second_round_temp = 0.8
    second_round_top_p = 0.7
    second_round_presence_penalty = 1

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    # 第一轮对话
    input_message_1 = Message(content=first_question, role="user")
    apifox_instance_1 = ApifoxModel(
        frequency_penalty=0.0,
        logit_bias=None,
        max_tokens=2048,
        messages=[input_message_1],
        model=model_id,
        n=1,
        presence_penalty=first_round_presence_penalty,
        response_format=None,
        seen=None,
        stop=None,
        stream=False,
        temperature=first_round_temp,
        tool_choice={},
        tools=[],
        top_p=first_round_top_p,
        user=None
    )
    params_dict_1 = apifox_instance_1.to_dict()
    response_first_round = requests.post(api_url, json=params_dict_1, headers=headers)

    if response_first_round.status_code != 200:
        print("第一轮未能从API获得响应,状态代码:", response_first_round.status_code)
        return None

    first_response_content = response_first_round.json()['choices'][0]['message']['content']
    print(first_response_content)

    # 第二轮对话
    input_message_2 = Message(content=second_question, role="user")
    messages_for_second_round = [input_message_1, Message(content=first_response_content, role="assistant"),
                                 input_message_2]

    apifox_instance_2 = ApifoxModel(
        frequency_penalty=0.0,
        logit_bias=None,
        max_tokens=2048,
        messages=messages_for_second_round,
        model=model_id,
        n=1,
        presence_penalty=second_round_presence_penalty,
        response_format=None,
        seen=None,
        stop=None,
        stream=False,
        temperature=second_round_temp,
        tool_choice={},
        tools=[],
        top_p=second_round_top_p,
        user=None
    )
    params_dict_2 = apifox_instance_2.to_dict()
    response_second_round = requests.post(api_url, json=params_dict_2, headers=headers)

    if response_second_round.status_code != 200:
        print("第二轮未能从API获得响应,状态代码:", response_second_round.status_code)
        return None

    second_response_content = response_second_round.json()['choices'][0]['message']['content']
    # print(second_response_content)

    return second_response_content


# 使用封装的函数
# first_question = "你是谁"
# second_question = "你能做什么"

# 调用函数进行两轮对话
# first_response, second_response = dialogue_with_model(first_question, second_question)


最后在影刀中写入指令即可完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值