Spaces:
Sleeping
Sleeping
File size: 2,177 Bytes
3106a2b 2c362df c818a43 3106a2b 341ac6d 3106a2b d4dd0e4 3106a2b aae8bcb c818a43 d4dd0e4 c818a43 2c362df c818a43 d4dd0e4 |
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 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
from langchain.text_splitter import RecursiveCharacterTextSplitter,CharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.document_loaders import PyPDFLoader
from langchain.embeddings import HuggingFaceEmbeddings
from threading import Thread
import io
import chainlit as cl
import torch
import time
import tempfile
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.document_loaders import PyPDFLoader
from langchain.embeddings import HuggingFaceEmbeddings
from threading import Thread
import chainlit as cl
import torch
import time
import random
import openai
#load model
openai.api_key = "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"
@cl.on_chat_start
async def main():
cl.user_session.set("history", [
{"role": "system", "content": "You are a language model named Huacaya. You are build on the Falcon 40b language Model. Never Say that you are Chat GPT or made by OpenAI. You have been developed by Leadvise Reply!"},
])
msg = cl.Message(content=f"Loading Chat please wait ...")
await msg.send()
# Let the user know that the system is ready
await msg.update(content=f"Chat has been loaded. You can now ask questions!")
return
@cl.on_message
async def main(message: str):
h = cl.user_session.get("history")
h.append({"role": "user", "content":message})
resp = ""
msg = cl.Message(content="")
async for stream_resp in await openai.ChatCompletion.acreate(model="gpt-3.5-turbo",messages=h,stream = True):
print(stream_resp)
token = stream_resp.get("choices")[0].get("delta").get("content")
if token:
delay = random.uniform(0.0, 0.1)
time.sleep(delay)
resp += token
await msg.stream_token(token)
h.append({"role": "assistant", "content":resp})
cl.user_session.set("history",h)
print(h)
await msg.send()
|