1、建立环境

mkdir doubao-py-server
cd doubao-py-server/
python3 -m venv .venv
source .venv/bin/activate

2、安装依赖

pip install volcengine-python-sdk
#用这个去启动
#建立环境
#python3 -m venv .venv
#激活环境
#source .venv/bin/activate
#服务启动
#uvicorn main:app --reload
#安装依赖
#pip install fastapi fastapi-cors uvicorn requests
#可能的一个bug
#不知道是不是误删了还是怎么了,长期运行,然后之间.venv不见了,可能是误删了,不管了

3、sk和ak那两个以及模型id

 https://www.volcengine.com/docs/82379/1263482 

权限记得搜索Ark

'''
Usage:
Ark v3 sdk
pip install 'volcengine-python-sdk[ark]'
'''
from volcenginesdkarkruntime import Ark
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List
# fetch ak&sk from environmental variables "VOLC_ACCESSKEY", "VOLC_SECRETKEY"
# or specify ak&sk by Ark(ak="${YOUR_AK}", sk="${YOUR_SK}").
# you can get ak&sk follow this document(https://www.volcengine.com/docs/6291/65568)
client = Ark(ak = "xxxxxxxxxxxxxxxxx",sk = "xxxxxxxxxxxxxxxxxx")
YOUR_ENDPOINT_ID = "xxxxxxxxxxxxxxxxxxxx"
app = FastAPI()
if __name__ == "__main__":
    # Non-streaming:
    print("----- standard request -----")
    completion = client.chat.completions.create(
        model="xxxxxxxxxxxx",
        messages=[
            {
                "role": "user",
                "content": "你好啊",
            },
        ]
    )
    print(completion.choices[0].message.content)
# 因为是xxxx发起的,所以需要要把抖音的这个地址加进去    
origins = [
    "https://blog.lemonhall.me",
]
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
class aMessage(BaseModel):
    content: str
@app.post("/v1/completions")
async def main(a_Message: aMessage):
    completion = client.chat.completions.create(
        model="ep-20240526112518-f2m5k",
        messages=[
            {
                "role": "user",
                "content": a_Message.content,
            },
        ]
    )
    respone = completion.choices[0].message.content
    print(respone)
    return respone
#用这个去启动
#建立环境
#python3 -m venv .venv
#激活环境
#source .venv/bin/activate
#服务启动
#uvicorn main:app --reload
#安装依赖
#pip install fastapi fastapi-cors uvicorn requests
#可能的一个bug
#不知道是不是误删了还是怎么了,长期运行,然后之间.venv不见了,可能是误删了,不管了


写的我真是脑壳痛

行了,然后开始调用实验


3、client端的代码如下


import {b as browser} from "./libs/ollama.js";
//import { b as browser } from './theModule';
// test.js
window.saySth = async function(my_choice) {
    var system_prompt = `你现在扮演一个游戏里的普通npc,对话的回复使用json格式,
    我先与你对话,并向你问好:"你好啊"
    你应该回复如下:
    {response:"你也好啊",choices:["你现在有空么?","没事情了"]}
    其中response是你的回复,choices则是你撰写的,我有可能的下一轮的问题。
    你拥有以下两个可能的action,
    所以你的action_list:["钓鱼":"当你在response当中有钓鱼的想法时,可以使用这个action能力","唱首歌":"当你在response中有其它想法的时候,使用这个action“]
    当你产生了这样的想法时候,请在回复时,带上接下来你将要做的action:
    比如:
    {"response":"我们可以去钓鱼呀","choices":["好呀,那就出发吧","还是算了吧"],next_action:["钓鱼"]}' }],`;
    var choices_list =[];
    if (my_choice!== undefined && my_choice!== null) {
        // 有值时的处理逻辑
      } else {
        // 外部调用者没传值时的处理逻辑
        my_choice = '你好啊'
      }
    // 函数的具体实现
    fetch('http://127.0.0.1:8000/v1/completions', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          content: my_choice
        }),
      })
      .then(response => response.json())
      .then(function(data){
                            console.log("NPC回复:"+data)
                            $gameMessage.add(data);
                            //TODO:Parser response to get a choice list
                            choices_list = ['有什么新鲜事么?', '天气真好啊', '没事'];
                            // Set choices to A, B, C; default C, no cancel
                            $gameMessage.setChoices(choices_list, 2, 2);
                            $gameMessage.setChoiceCallback(async n => {
                                console.log("我选择了n:"+n);
                                my_choice = choices_list[n];
                                console.log("我的选择:"+my_choice)
                                if(n == 2){
                                    $gameMessage.add("好的,回见")
                                }else{
                                    saySth(my_choice);
                                }
                                    // // 第二轮对话
                                    // const response2 = await browser.chat({
                                    //     model: 'llama2-uncensored:latest',
                                    //     messages: [{ role: 'user', content: '有什么新鲜事么?'}],
                                    // });
                                    // console.log(response2.message.content)
                                    // $gameMessage.add(response2.message.content);
                                    // // Set choices to A, B, C; default C, no cancel
                                    // $gameMessage.setChoices(['啊,那真有趣啊', '你真有趣', '没事'], 2, 2);
                            });     
       })
      .catch(error => console.error('出错了:', error));
};
// 你现在扮演一个游戏里的普通npc,对话的回复使用json格式,
// 我先与你对话,并向你问好:"你好啊"
// 你应该回复如下:
// {response:"你也好啊",choices:["你现在有空么?","没事情了"]}
// 其中response是你的回复,choices则是你撰写的,我有可能的下一轮的问题。
// 你拥有以下两个可能的action,
// 所以你的action_list:["钓鱼":"当你在response当中有钓鱼的想法时,可以使用这个action能力","唱首歌":"当你在response中有其它想法的时候,使用这个action“]
// 当你产生了这样的想法时候,请在回复时,带上接下来你将要做的action:
// 比如:
// {"response":"我们可以去钓鱼呀","choices":["好呀,那就出发吧","还是算了吧"],next_action:["钓鱼"]}