AllMhub
  1. 部分AI应用配置方法
AllMhub
  • 关于AllMHub
  • 大模型API有啥用?
  • 平台使用教程
    • 注册及菜单介绍
    • 卡密购买及兑换
    • 令牌(API KEY)的创建
    • 站内直接使用
    • API接口文档(开发者使用)
  • 部分AI应用配置方法
    • 【必读】API地址填什么?
    • Python中直接调用
    • 【移动端推荐】Chatbox AI
    • 【桌面应用推荐】CherryStudio
    • NextChat
    • Sider
    • 【写代码推荐】Cursor
    • 【工作流推荐】Dify
    • 【翻译推荐】沉浸式翻译
    • 【角色扮演推荐】SillyTavern
    • Omate Chat
    • Tavo
  • 联系我们
    • 客服联系方式
    • 售后Q群
    • 我是企业/高校客户
  • 常见问题整理
    • 新手常见问题
    • 老手进阶问题
    • 其他问题
    • 常见错误码
  1. 部分AI应用配置方法

Python中直接调用

Langchain中使用#

注意:openai_api_base 的末尾要加上/v1/chat/completions,而且目前只支持 Chat 模型,请确认不要导入了错误的包。
from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(
    openai_api_base="https://api.allmhub.com/v1/",    openai_api_key="sk-xxxxx",
)

res = llm.predict("hello")

print(res)

官方 openai 库中使用#

import openai

def query_gpt4(question):
    openai.api_key = "sk-xxx"
    #openai.base_url = url
    openai.base_url = 'https://api.allmhub.com/v1/'


    try:
        response = openai.chat.completions.create(
            model="gpt-4",  # 确认使用 GPT-4 模型
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": question}
            ]
        )
        print(response)
        return response['choices'][0].message['content']
    except Exception as e:
        return str(e)

# 问题
question = "为什么太阳那么红?"

# 获取并打印回答
answer = query_gpt4(question)
print(answer)

# 使用方法
若出现module 'openai' has no attribute 'chat'报错,请更新openai库:pip3 install --upgrade openai
若出现module 'attr' has no attribute 'frozen'报错,请更新输入:pip3 install --upgrade attrs

通过request非流式请求示例(chat示例)#

import requests
import json
def get_chat_gpt_response(prompt):
    url = "https://api.allmhub.com/v1/chat/completions"
    headers = {
        "Authorization": "Bearer apikey",
        "Content-Type": "application/json"
    }
    data = {
        "model": "claude-3-haiku-20240307",
        "messages": [{"role": "user", "content": prompt}]
    }

    response = requests.post(url, headers=headers, json=data)
    print(json.loads(response.text))
    return response

resp = get_chat_gpt_response('给我画一个可爱的女生穿着绿色的卫衣')
print(resp.text)

通过 流式请求示例(chat示例)#

import http.client
import json

conn = http.client.HTTPSConnection("api.allmhub.com")
payload = json.dumps({
   "model": "gpt-3.5-turbo",
   "messages": [
      {
         "role": "system",
         "content": "You are a helpful assistant."
      },
      {
         "role": "user",
         "content": "Hello!"
      }
   ],
   "stream": True
})
headers = {
   'Accept': 'application/json',
   'Authorization': 'Bearer {{YOUR_API_KEY}}',
   'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
   'Content-Type': 'application/json'
}
conn.request("POST", "/v1/chat/completions", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

通request请求示例(图片分析示例)#

import requests
import json

def analyze_image(image_url, api_url, openai_key):
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {openai_key}'
    }

    data = {
        "model": "gpt-4-vision-preview",
        "messages": [
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": "What’s in this image?"},
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": image_url,
                        },
                    },
                ],
            }
        ],
        "max_tokens": 4000,
    }

    response = requests.post(api_url, headers=headers, data=json.dumps(data))

    print(response.json())

# 使用方法
analyze_image("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", "https://api.allmhub.com/v1/chat/completions", "sk-xxxxxxx")
修改于 2025-07-08 07:41:06
上一页
【必读】API地址填什么?
下一页
【移动端推荐】Chatbox AI
Built with