|
1 | 1 | import openai
|
2 |
| -openai.api_key = "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" |
3 |
| - |
4 |
| -response = openai.ChatCompletion.create( |
5 |
| - model="gpt-3.5-turbo", |
6 |
| - messages=[ |
7 |
| - {"role": "system", "content": "You are useful assistant"}, |
8 |
| - {"role": "user", "content": "Can you tell top 10 startups in India"}, |
9 |
| - {"role": "assistant", "content": "1. Flipkart \n2. Ola Cabs \n3. Paytm \n4. BYJU's \n5. Swiggy \n6. Zomato \n7. Udaan \n8. Freshworks \n9. Nykaa \n10. Policybazaar"}, |
10 |
| - {"role": "user", "content": "That is good but can you tell top 10 early stage strtups? some of these re late stage...."}, |
11 |
| - |
12 |
| - ] |
13 |
| -) |
| 2 | +import time |
| 3 | +from functools import wraps |
| 4 | +import requests |
| 5 | + |
| 6 | + |
| 7 | +def retry(tries=4, delay=3, backoff=2): |
| 8 | + """ |
| 9 | + Retries a function or method until it succeeds or the number of retries is exceeded. |
| 10 | +
|
| 11 | + :param tries: the maximum number of times to retry (default 4). |
| 12 | + :param delay: the initial delay between retries in seconds (default 3). |
| 13 | + :param backoff: the backoff multiplier (e.g. value of 2 will double the delay each retry) (default 2). |
| 14 | + """ |
| 15 | + |
| 16 | + def deco_retry(func): |
| 17 | + @wraps(func) |
| 18 | + def f_retry(*args, **kwargs): |
| 19 | + mtries, mdelay = tries, delay |
| 20 | + while mtries > 1: |
| 21 | + try: |
| 22 | + return func(*args, **kwargs) |
| 23 | + except ( |
| 24 | + openai.error.APIError, |
| 25 | + requests.exceptions.RequestException, |
| 26 | + ) as e: |
| 27 | + print(f"Error occurred: {str(e)}") |
| 28 | + time.sleep(mdelay) |
| 29 | + mtries -= 1 |
| 30 | + mdelay *= backoff |
| 31 | + return func(*args, **kwargs) |
| 32 | + |
| 33 | + return f_retry |
| 34 | + |
| 35 | + return deco_retry |
| 36 | + |
| 37 | + |
| 38 | +def dot_product(list1, list2): |
| 39 | + # Check if the lengths of the two lists are equal |
| 40 | + if len(list1) != len(list2): |
| 41 | + raise ValueError("Lists must have same length") |
| 42 | + |
| 43 | + # Calculate the dot product using a loop |
| 44 | + dot_product = 0 |
| 45 | + for i in range(len(list1)): |
| 46 | + dot_product += list1[i] * list2[i] |
| 47 | + |
| 48 | + return dot_product |
| 49 | + |
| 50 | + |
| 51 | +class ChatGptSmartClient(object): |
| 52 | + """ |
| 53 | + This is a wrapper class for the chatgpt python api, |
| 54 | + it is meant to provide developers a smooth expereince in |
| 55 | + developing chatgpt applications of their own without the need |
| 56 | + for worrying about things like retries, tracking message history |
| 57 | + or storing messages. This runs on top of the Chat APIs provided by OpenAI |
| 58 | + read more on that here: https://platform.openai.com/docs/guides/chat . |
| 59 | + """ |
| 60 | + |
| 61 | + def __init__(self, api_key: str, model: str): |
| 62 | + openai.api_key = api_key |
| 63 | + |
| 64 | + self.instruction_msgs = { |
| 65 | + "role": "system", |
| 66 | + "content": "You are a useful assistant", |
| 67 | + } |
| 68 | + self.prev_msgs = [self.instruction_msgs] |
| 69 | + self.model = model |
| 70 | + |
| 71 | + @retry() |
| 72 | + def query(self, query: str, w_context=True, add_to_context=True): |
| 73 | + # TODO: We coud get the embeddings, cache and further use them to speed up the results. |
| 74 | + # self.get_embeddings(query=query) |
| 75 | + |
| 76 | + query = {"role": "user", "content": query} |
| 77 | + |
| 78 | + if w_context: |
| 79 | + msgs = self.prev_msgs[:] |
| 80 | + msgs.append(query) |
| 81 | + response = openai.ChatCompletion.create(model=self.model, messages=msgs) |
| 82 | + else: |
| 83 | + msgs = [self.instruction_msgs, query] |
| 84 | + response = openai.ChatCompletion.create(model=self.model, messages=msgs) |
| 85 | + |
| 86 | + f_resp = response["choices"][0]["message"] |
| 87 | + |
| 88 | + if add_to_context: |
| 89 | + self.prev_msgs.append(f_resp) |
| 90 | + |
| 91 | + return f_resp |
| 92 | + |
| 93 | + def erase_history(self): |
| 94 | + self.prev_msgs = [self.instruction_msgs] |
| 95 | + |
| 96 | + # This function is used for getting embeddings and hence maybe |
| 97 | + # used to speedup the system by caching. |
| 98 | + def get_embeddings(self, query: str): |
| 99 | + response = openai.Embedding.create(input=query, model="text-embedding-ada-002") |
| 100 | + embeddings = response["data"][0]["embedding"] |
| 101 | + print(len(embeddings)) |
| 102 | + |
| 103 | + def rollback_conversation(self): |
| 104 | + pass |
0 commit comments