File size: 3,488 Bytes
59e8724
1d7aa8d
2af54dc
 
17e4567
 
dd9f46c
c482f94
17e4567
 
 
dd9f46c
17e4567
 
 
 
c482f94
17e4567
 
 
c482f94
5089ee0
 
17e4567
 
be83449
17e4567
be83449
17e4567
 
be83449
 
17e4567
 
 
 
 
 
 
333a86e
 
 
 
 
 
 
17e4567
333a86e
17e4567
 
 
 
 
 
 
 
94639bb
 
 
 
 
333a86e
94639bb
6765a9d
d7933bc
 
1c696bd
17e4567
 
 
9ab3547
17e4567
 
1a96d1f
6765a9d
1a96d1f
17e4567
8845b69
59e8724
e05e51c
8845b69
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#import uvicorn
#import streamlit as st
import flask
#之前没有import flask
from flask import Flask, request, jsonify
from langchain import PromptTemplate, LLMChain
#from langchain.memory import StreamlitChatMessageHistory
#import numpy as np
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory
#from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
from langchain import HuggingFaceHub
import os
from dotenv import load_dotenv
load_dotenv()
#from pathlib import Path
from huggingface_hub import InferenceClient
from langchain import HuggingFaceHub
import requests
#from pydantic import BaseModel
import uuid
import sys

# 初始化Chatbot
hf_token = os.environ.get('HUGGINGFACEHUB_API_TOKEN')
repo_id = os.environ.get('repo_id')
#port = os.getenv('port')

llm = HuggingFaceHub(repo_id=repo_id,
                     #huggingfacehub_api_token="hf_p***K",
                     huggingfacehub_api_token=hf_token,
                     model_kwargs={"min_length":1024,
                                   "max_new_tokens":5632, "do_sample":True,
                                   "temperature":0.1,
                                   "top_k":50,
                                   "top_p":0.95, "eos_token_id":49155}) 


#prompt_template = """
#<<SYS>>You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.  Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
#If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
#In each conversation, question is placed after [INST] while your answer should be placed after [/INST].<</SYS>>
#[INST] {user_question} [/INST]
#assistant:
#"""
prompt_template = """
<<SYS>>You are a helpful, respectful and honest assistant. If you don't know the answer to a question, please don't share false information.In each conversation, question is placed after [INST] while your answer should be placed after [/INST].<</SYS>>
[INST] {user_question} [/INST]
assistant:
"""

llm_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template(prompt_template))

# 定义API端点
app = Flask(__name__)
@app.route('/', methods=['POST'])
def home_api():
    data = request.get_json()
    user_query = data['user_question']
    print(user_query)
    return {"Message":"Flask Home API Deploy Success on HF"}  

@app.route('/api/chat', methods=['POST'])
def chat():
#async def chat():
#不支持async    
    data = request.get_json()
    #user_query = data['query']
#此处的['query']中的query可以自定义名称,例如修改为user_question,那么调用API的代码中,需要相应的使用data = {'user_question': user_query},user_question需一致
    user_query = data['user_question']
    # 调用Chatbot
    initial_response = llm_chain.run(user_query)
    #return jsonify({'response': initial_response})
    #找到问题了:jsonify在Huggingface不支持;在Github然后部署到Render是可以的!
    return {'response': initial_response}

#if __name__ == "__main__":
#    uvicorn.run(app, host='0.0.0.0', port=5000) 
    #app.run(host='0.0.0.0')
    #app.run(host='0.0.0.0', port=10000)