新鲜出炉的小工具,将Claude 100K转化为免费可用的OpenAI API

上个月转载了一篇文章,讲的就是刚刚发布的Claude 2.0,可以说是非常强大了:ChatGPT最强竞品Claude2来了:代码、GRE成绩超越GPT-4,免费可用

但是可惜的是,Claude虽然免费使用,但是不开放API给我们用,为了能自动化的使用Claude 100K的能力,开发者@easychen开发了一个小工具,能够轻松将Claude 网站转换为 OpenAI API,我们本地的软件和程序可以直接调用

AiAPI:GitHub - easychen/aiapi: A Claude-driven, OpenAI specification-compliant API, free

AiAPI是一个跨平台客户端,它可以将 Claude 网站转化为 OpenAI 兼容的 API,这样你就可以在所有兼容 OpenAI 的软件里边免费使用 Claude 的 100k 上下文能力

我也是在第一时间下载并且测试了这个软件,下面讲一下使用方法

首先,一切的前提是你能够正常访问Claude并有自己的账号

https://claude.ai

登录自己的账号,并来到聊天页面

https://claude.ai/chats

按下F12打开DevTools - Applications - Cookies - 复制sessionKey

图片

网页这部分的操作就结束了,下面开始研究一下这个软件


在Github的Release页面下载最新的软件,目前支持Win和Mac系统,需要解压并运行软件

将Setting页面的【Claude Session Key】内容修改为上面你复制的Key,如果你的电脑是通过代理来访问Claude的话,需要将【Http proxy】修改为你的代理地址,然后点击【Save】

图片

如果不会找代理地址的话,可以参考这个教程,不大一样,但可以借鉴

https://github.com/binary-husky/gpt_academic/issues/1

此时我们就可以使用本地的API endpoint了,在你需要使用OpenAI API的程序中,原来填写OpenAI API Endpoint 的地方改为 http://127.0.0.1:3456 即可(如果原来后边有/v1/...,那么把这部分也加上)

图片

下面是测试环节,由于没有现成的程序可以用,我就自己写了个Python程序,调用本地的API endpoint来与之对话:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import requests

# 填写 OpenAI API Endpoint 和 API Key
API_ENDPOINT = "http://127.0.0.1:3456/v1/chat/completions"
API_KEY = "sk-1234567890"

def main():
    while True:
        user_input = input("您: ")
        if user_input.lower() == 'exit':
            break
        response = generate_response(user_input)
        if response is not None:
            print("ChatGPT:", response)

def generate_response(user_input):
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {API_KEY}"
    }
    data = {
        "messages": [{"role": "user", "content": user_input}]
    }
    
    try:
        response = requests.post(API_ENDPOINT, headers=headers, json=data)
        response.raise_for_status() 
        try:
            response_json = response.json()
            if "choices" in response_json:
                chat_output = response_json["choices"][0]["message"]["content"]
                return chat_output
            else:
                print("出现了错误,请检查您的 API 设置或稍后重试。")
                return None
        except ValueError as e:
            print("无法解析 JSON 响应:", e)
            return None
    except requests.exceptions.RequestException as e:
        print("请求异常:", e)
        return None

if __name__ == "__main__":
    main()

在命令行中进行测试,效果还是蛮好的

图片

猜你喜欢

转载自blog.csdn.net/weixin_45498383/article/details/132249240