当前位置:   article > 正文

简单的chatGPT 网页界面

简单的chatGPT 网页界面

 

        大语言模型的应用大多数采用python 语言实现,为了与其它应用结合,最简单的方法就是采样网页 RESTful API 服务。目前流行的Python 的Web 服务是FastAPI。本文记录了一个简单LLM 对话FastAPI 的网站服务的实验过程。

界面

安装

安装如下两个主要模块

  1. pip install fastapi
  2. pip install uvicorn

文件目录结构

FastAPI 具有固定的目录结构,Templates 中包含了index.hml

static 文件夹

,static 中包含了js,css,images等文件夹。

主程序(main.py)

  1. import asyncio
  2. import nest_asyncio
  3. from langchain_core.prompts import PromptTemplate
  4. from langchain.chains import LLMChain
  5. from langchain.chat_models import ErnieBotChat
  6. from pydantic import BaseModel
  7. nest_asyncio.apply()
  8. llm= ErnieBotChat(model_name='ERNIE-Bot', #ERNIE-Bot
  9. ernie_client_id='xxxxxxxx',
  10. ernie_client_secret='xxxxxxxxxxx',
  11. temperature=0.75,
  12. )
  13. template = """You are a nice chatbot having a conversation with a human.
  14. New human question: {question}
  15. Response:"""
  16. prompt = PromptTemplate.from_template(template)
  17. # Notice that we need to align the `memory_key`
  18. conversation = LLMChain(
  19. llm=llm,
  20. prompt=prompt,
  21. verbose=True,
  22. )
  23. class Prompt(BaseModel):
  24. Method: str
  25. Message:str
  26. app = FastAPI()
  27. app.mount("/static", StaticFiles(directory="static"), name="static")
  28. templates = Jinja2Templates(directory="templates")
  29. @app.get("/")
  30. async def root(request: Request):
  31. # return {"message": "Hello, World!"}
  32. return templates.TemplateResponse("index.html",{
  33. "request": request
  34. })
  35. @app.post("/generate/")
  36. def generate(prompt:Prompt):
  37. print(prompt)
  38. AIresponse=conversation.predict(question=prompt.Message)
  39. response=prompt
  40. response.Message=AIresponse
  41. print(response)
  42. return {"response": response}
  43. async def run_server():
  44. uvicorn.run(app, host="localhost", port=8000)
  45. if __name__ == "__main__":
  46. loop = asyncio.get_event_loop()
  47. loop.run_until_complete(run_server())

注意:search_kwargs 很重要

search_kwargs 是搜索文档的数量, 当您将“k”设置为 2 时,检索器将仅返回前 2 个最相关的文档。如果将“k”设置为 3,它将返回前 3 个最相关的文档。在memory中,每一条对话是一个文档。所有要K要设置大一点,可以引用多条对话,获取历史信息。

网页:

前端使用bootstrap 架构,设计了chat.css.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>FastAPI Example</title>
  5. <link rel="stylesheet" href="static/css/bootstrap.min.css">
  6. <link rel="stylesheet" href="static/css/chat.css">
  7. <link rel="stylesheet" href="static/css/font-awesome.min.css">
  8. <link rel="stylesheet" href="static/font/bootstrap-icons.css">
  9. <script src="static/js/jquery-3.6.0.min.js"></script>
  10. <script src="static/js/bootstrap.min.js"></script>
  11. <script src="static/js/moment.js"></script>
  12. <style>
  13. .form-outline i {
  14. position: absolute;
  15. top: 50%;
  16. transform: translateY(-50%);
  17. pointer-events: none;
  18. }
  19. .chat {
  20. height: 480px;
  21. overflow-y: scroll;
  22. }
  23. </style>
  24. <script>
  25. function InsertPrompt(message) {
  26. var message_li = document.createElement("li");
  27. message_li.setAttribute("class", "clearfix");
  28. var message_data = document.createElement("div")
  29. message_data.setAttribute("class", "message-data");
  30. var message_data_time = document.createElement("span")
  31. message_data_time.setAttribute("class", "message-data-time")
  32. const timeElapsed = Date.now();
  33. const today = new Date(timeElapsed);
  34. message_data_time.innerText = moment(today).format('LLL')
  35. message_data.appendChild(message_data_time);
  36. var message_content = document.createElement("div")
  37. message_content.setAttribute("class", "message my-message")
  38. message_content.innerText = message;
  39. message_li.appendChild(message_data)
  40. message_li.appendChild(message_content)
  41. $("#chat").append(message_li)
  42. }
  43. function InsertAIResponse(message) {
  44. var message_li = document.createElement("li");
  45. message_li.setAttribute("class", "clearfix");
  46. var message_data = document.createElement("div")
  47. message_data.setAttribute("class", "message-data text-right");
  48. var message_data_time = document.createElement("span")
  49. message_data_time.setAttribute("class", "message-data-time")
  50. const timeElapsed = Date.now();
  51. const today = new Date(timeElapsed);
  52. message_data_time.innerText = moment(today).format('LLL')
  53. message_data.appendChild(message_data_time);
  54. var message_image = document.createElement("img")
  55. message_image.setAttribute("src", "static/images/chatGPT.jpg")
  56. message_image.setAttribute("alt", "avatar")
  57. message_data.appendChild(message_image);
  58. var message_block = document.createElement("div")
  59. message_block.setAttribute("class","d-flex justify-content-end")
  60. var message_content = document.createElement("div")
  61. message_content.setAttribute("class", "message other-message align-items-start ")
  62. message_content.innerText = message;
  63. message_block.append(message_content)
  64. message_li.appendChild(message_data)
  65. message_li.appendChild(message_block)
  66. $("#chat").append(message_li)
  67. }
  68. function Send() {
  69. console.log("Send");
  70. messageContent = $("#Message").val();
  71. InsertPrompt(messageContent)
  72. console.log(messageContent)
  73. var parameter = {
  74. Method: "SendMessage",
  75. Message: messageContent
  76. }
  77. $.ajax({
  78. url: '/generate',
  79. type: 'post',
  80. contentType: "application/json",
  81. dataType: "json",
  82. data: JSON.stringify(parameter),
  83. success: function (res) {
  84. response = res.response
  85. console.log(response.Message);
  86. InsertAIResponse(response.Message)
  87. $('#scroll-to-bottom').scrollTop($('#scroll-to-bottom')[0].scrollHeight);
  88. }
  89. });
  90. }
  91. </script>
  92. </head>
  93. <body>
  94. <div class="container">
  95. <div class="row clearfix">
  96. <div class="col-lg-8">
  97. <h2 class="text-info">LLM</h2>
  98. <div class="card ">
  99. <div class="chat" id="scroll-to-bottom">
  100. <div class="chat-history">
  101. <ul class="m-b-0" id="chat">
  102. <li class="clearfix">
  103. <div class="message-data text-right">
  104. <span class="message-data-time">10:10 AM, Today</span>
  105. <img src="static/images/chatGPT.jpg" alt="avatar">
  106. </div>
  107. <div class="text-right">
  108. <div class="message other-message "> 你好!,我有什么能帮到你? </div>
  109. </div>
  110. </li>
  111. </ul>
  112. </div>
  113. </div>
  114. </div>
  115. </div>
  116. <div class="col-lg-4">
  117. <h2 class="text-info">Message</h2>
  118. <textarea class="form-control" id="Message" rows="10" placeholder="Message *"
  119. style="line-height:initial"></textarea>
  120. <div class="text-right">
  121. <button class="btn btn-info right-aligent" style="margin: 10px;" onclick="Send()">Send</button>
  122. </div>
  123. </div>
  124. </div>
  125. </div>
  126. </body>
  127. </html>

css(chat.css)

  1. body{
  2. background-color: #f4f7f6;
  3. margin-top:20px;
  4. }
  5. .card {
  6. background: #fff;
  7. transition: .5s;
  8. border: 0;
  9. margin-bottom: 30px;
  10. border-radius: .55rem;
  11. position: relative;
  12. width: 100%;
  13. box-shadow: 0 1px 2px 0 rgb(0 0 0 / 10%);
  14. }
  15. .chat-app .people-list {
  16. width: 280px;
  17. position: absolute;
  18. left: 0;
  19. top: 0;
  20. padding: 20px;
  21. z-index: 7
  22. }
  23. .chat-app .chat {
  24. margin-left: 280px;
  25. border-left: 1px solid #eaeaea
  26. }
  27. .people-list {
  28. -moz-transition: .5s;
  29. -o-transition: .5s;
  30. -webkit-transition: .5s;
  31. transition: .5s
  32. }
  33. .people-list .chat-list li {
  34. padding: 10px 15px;
  35. list-style: none;
  36. border-radius: 3px
  37. }
  38. .people-list .chat-list li:hover {
  39. background: #efefef;
  40. cursor: pointer
  41. }
  42. .people-list .chat-list li.active {
  43. background: #efefef
  44. }
  45. .people-list .chat-list li .name {
  46. font-size: 15px
  47. }
  48. .people-list .chat-list img {
  49. width: 45px;
  50. border-radius: 50%
  51. }
  52. .people-list img {
  53. float: left;
  54. border-radius: 50%
  55. }
  56. .people-list .about {
  57. float: left;
  58. padding-left: 8px
  59. }
  60. .people-list .status {
  61. color: #999;
  62. font-size: 13px
  63. }
  64. .chat .chat-header {
  65. padding: 15px 20px;
  66. border-bottom: 2px solid #f4f7f6
  67. }
  68. .chat .chat-header img {
  69. float: left;
  70. border-radius: 40px;
  71. width: 40px
  72. }
  73. .chat .chat-header .chat-about {
  74. float: left;
  75. padding-left: 10px
  76. }
  77. .chat .chat-history {
  78. padding: 20px;
  79. border-bottom: 2px solid #fff
  80. }
  81. .chat .chat-history ul {
  82. padding: 0
  83. }
  84. .chat .chat-history ul li {
  85. list-style: none;
  86. margin-bottom: 30px
  87. }
  88. .chat .chat-history ul li:last-child {
  89. margin-bottom: 0px
  90. }
  91. .chat .chat-history .message-data {
  92. margin-bottom: 15px
  93. }
  94. .chat .chat-history .message-data img {
  95. border-radius: 40px;
  96. width: 40px
  97. }
  98. .chat .chat-history .message-data-time {
  99. color: #434651;
  100. padding-left: 6px
  101. }
  102. .chat .chat-history .message {
  103. color: #444;
  104. padding: 18px 20px;
  105. line-height: 26px;
  106. font-size: 16px;
  107. border-radius: 7px;
  108. display: inline-block;
  109. position: relative
  110. }
  111. .chat .chat-history .message:after {
  112. bottom: 100%;
  113. left: 7%;
  114. border: solid transparent;
  115. content: " ";
  116. height: 0;
  117. width: 0;
  118. position: absolute;
  119. pointer-events: none;
  120. border-bottom-color: #fff;
  121. border-width: 10px;
  122. margin-left: -10px
  123. }
  124. .chat .chat-history .my-message {
  125. background: #efefef
  126. }
  127. .chat .chat-history .my-message:after {
  128. bottom: 100%;
  129. left: 30px;
  130. border: solid transparent;
  131. content: " ";
  132. height: 0;
  133. width: 0;
  134. position: absolute;
  135. pointer-events: none;
  136. border-bottom-color: #efefef;
  137. border-width: 10px;
  138. margin-left: -10px
  139. }
  140. .chat .chat-history .other-message {
  141. background: #e8f1f3;
  142. text-align: right
  143. }
  144. .chat .chat-history .other-message:after {
  145. border-bottom-color: #e8f1f3;
  146. left: 93%
  147. }
  148. .chat .chat-message {
  149. padding: 20px
  150. }
  151. .online,
  152. .offline,
  153. .me {
  154. margin-right: 2px;
  155. font-size: 8px;
  156. vertical-align: middle
  157. }
  158. .online {
  159. color: #86c541
  160. }
  161. .offline {
  162. color: #e47297
  163. }
  164. .me {
  165. color: #1d8ecd
  166. }
  167. .float-right {
  168. float: right
  169. }
  170. .clearfix:after {
  171. visibility: hidden;
  172. display: block;
  173. font-size: 0;
  174. content: " ";
  175. clear: both;
  176. height: 0
  177. }
  178. @media only screen and (max-width: 767px) {
  179. .chat-app .people-list {
  180. height: 465px;
  181. width: 100%;
  182. overflow-x: auto;
  183. background: #fff;
  184. left: -400px;
  185. display: none
  186. }
  187. .chat-app .people-list.open {
  188. left: 0
  189. }
  190. .chat-app .chat {
  191. margin: 0
  192. }
  193. .chat-app .chat .chat-header {
  194. border-radius: 0.55rem 0.55rem 0 0
  195. }
  196. .chat-app .chat-history {
  197. height: 300px;
  198. overflow-x: auto
  199. }
  200. }
  201. @media only screen and (min-width: 768px) and (max-width: 992px) {
  202. .chat-app .chat-list {
  203. height: 650px;
  204. overflow-x: auto
  205. }
  206. .chat-app .chat-history {
  207. height: 600px;
  208. overflow-x: auto
  209. }
  210. }
  211. @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: landscape) and (-webkit-min-device-pixel-ratio: 1) {
  212. .chat-app .chat-list {
  213. height: 480px;
  214. overflow-x: auto
  215. }
  216. .chat-app .chat-history {
  217. height: calc(100vh - 350px);
  218. overflow-x: auto
  219. }
  220. }

与大语言模型的连接

(待续)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/594444
推荐阅读
相关标签
  

闽ICP备14008679号