Prakh24s commited on
Commit
ddead39
·
verified ·
1 Parent(s): 30f4bcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py CHANGED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+ import pprint
5
+ import chromadb
6
+ from chromadb.utils.embedding_functions import OpenAIEmbeddingFunction
7
+
8
+ # Load environment variables
9
+ client = OpenAI(api_key=os.getenv("OPENAI_KEY"))
10
+ pp = pprint.PrettyPrinter(indent=4)
11
+
12
+ def generate_response(messages):
13
+ model_name = os.getenv("MODEL_NAME")
14
+ response = client.chat.completions.create(model=model_name, messages=messages, temperature=0.5, max_tokens=250)
15
+ spinner.stop()
16
+ print("Request:")
17
+ pp.pprint(messages)
18
+ print(f"Completion tokens: {response.usage.completion_tokens}, Prompt tokens: {response.usage.prompt_tokens}, Total tokens: {response.usage.total_tokens}")
19
+ return response.choices[0].message
20
+
21
+ def chat_interface(user_input):
22
+ chroma_client = chromadb.Client()
23
+ embedding_function = OpenAIEmbeddingFunction(api_key=os.getenv("OPENAI_KEY"), model_name=os.getenv("EMBEDDING_MODEL"))
24
+ collection = chroma_client.create_collection(name="conversations", embedding_function=embedding_function)
25
+
26
+ messages = [{"role": "system", "content": "You are a kind and friendly chatbot"}]
27
+ results = collection.query(query_texts=[user_input], n_results=2)
28
+ for res in results['documents'][0]:
29
+ messages.append({"role": "user", "content": f"previous chat: {res}"})
30
+ messages.append({"role": "user", "content": user_input})
31
+
32
+ response = generate_response(messages)
33
+ return response
34
+
35
+ def main():
36
+ interface = gr.Interface(fn=chat_interface, inputs="text", outputs="text", title="Chatbot Interface")
37
+ interface.launch()
38
+
39
+ if __name__ == "__main__":
40
+ main()