File size: 4,527 Bytes
4e0c974 2edeee0 bdaa2e2 2edeee0 4e0c974 ecb3b61 bdaa2e2 2edeee0 e046093 96655e4 bdaa2e2 2edeee0 bdaa2e2 2edeee0 bdaa2e2 2edeee0 4e0c974 bdaa2e2 d396a7d bdaa2e2 2edeee0 bdaa2e2 2edeee0 7df9f15 88c5489 4e0c974 2edeee0 4e0c974 88c5489 2edeee0 bdaa2e2 2edeee0 bdaa2e2 e046093 96655e4 bdaa2e2 6f54b8e bdaa2e2 6f54b8e bdaa2e2 6f54b8e 2edeee0 bdaa2e2 e046093 bdaa2e2 2edeee0 bdaa2e2 7df9f15 2edeee0 d363ee6 |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
import aiohttp
import asyncio
import json, os
import yaml
import google.generativeai as palm
from App.Embedding.utils.Initialize import search
PALM_API = ""
API_KEY = os.environ.get("PALM_API", PALM_API)
GroqAPIKey = os.environ.get("Groq_API", "")
# palm.configure(api_key=API_KEY)
class GenerativeAIAssistant:
def __init__(
self,
api_key=API_KEY,
model="gemini-pro",
temperature=0.85,
candidate_count=1,
top_k=40,
top_p=0.95,
):
self.api_key = api_key
self.model = model
self.temperature = temperature
self.candidate_count = candidate_count
self.top_k = top_k
self.top_p = top_p
self.examples = [
{
"input": {"content": "hello"},
"output": {"content": "Hello to you too! How can I help you today?"},
}
]
self.context = "You are a helpful assistant"
def generate_template(self, question, task_id, summary=None):
if summary == None:
self.context = "You are a helpful assistant"
else:
self.context = summary
contexts = search(question, task_id=task_id)
context_yaml = ""
for context in contexts:
context_yaml += "\n" + yaml.dump(context)
Template = f"""
#Instructions
You are given the following context in yaml of a transcript of a youtube video, the start and end times are indicated and the text that was said is also given. You are also given a question, use the context to answer the question in a consise manner, make it short and to the point, don't provide additional details. If its just a conversational question,ignore the context, respond with the appropriate response. eg Hello hi, how are you? etc.
#Context
{context_yaml}
#Your response be as short as possible and to the point.
#Question
{question}
"""
return Template
async def generate_message(self, messages, task_id="ok"):
user_message = messages[-1]
# latest_message = messages[-1]["parts"][0]["text"]
user_message["content"]=self.generate_template(user_message["content"], task_id)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {GroqAPIKey}",
}
# user_message["parts"][0]["text"] = latest_message
messages[-1]=user_message
# url = f'https://generativelanguage.googleapis.com/v1beta/models/{self.model}:generateContent?key={self.api_key}'
url = "https://api.groq.com/openai/v1/chat/completions"
payload = {
"messages": messages,
"model": "mixtral-8x7b-32768",
}
# payload = {
# "contents": messages,
# "generationConfig": {
# "temperature": 0.9,
# "topK": 1,
# "topP": 1,
# "maxOutputTokens": 2048,
# "stopSequences": [],
# },
# "safetySettings": [
# {
# "category": "HARM_CATEGORY_HARASSMENT",
# "threshold": "BLOCK_ONLY_HIGH",
# },
# {
# "category": "HARM_CATEGORY_HATE_SPEECH",
# "threshold": "BLOCK_ONLY_HIGH",
# },
# {
# "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
# "threshold": "BLOCK_ONLY_HIGH",
# },
# {
# "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
# "threshold": "BLOCK_ONLY_HIGH",
# },
# ],
# }
# data = {
# "prompt": {
# "context": self.context,
# "examples": self.examples,
# "messages": messages
# },
# "temperature": self.temperature,
# "top_k": self.top_k,
# "top_p": self.top_p,
# "candidate_count": self.candidate_count
# }
async with aiohttp.ClientSession() as session:
async with session.post(
url, json=payload, headers=headers
) as response:
try:
temp = await response.json()
return temp["choices"][0]['message']["content"]
except Exception as e:
return f"Error ⚠️ {e} {temp}"
|