| 12 | |
| 13 | |
| 14 | class Davinci: |
| 15 | def __init__(self, model="text-davinci-003", openai_key="") -> None: |
| 16 | super().__init__() |
| 17 | self.model = model |
| 18 | self.openai_key = openai_key |
| 19 | self.chatio = SimpleChatIO() |
| 20 | |
| 21 | def prediction(self, prompt: str, stop: Optional[List[str]] = None) -> str: |
| 22 | max_try = 10 |
| 23 | while True: |
| 24 | openai.api_key = self.openai_key |
| 25 | try: |
| 26 | response = openai.Completion.create( |
| 27 | engine=self.model, |
| 28 | prompt=prompt, |
| 29 | temperature=0.5, |
| 30 | max_tokens=512, |
| 31 | top_p=1, |
| 32 | frequency_penalty=0, |
| 33 | presence_penalty=0, |
| 34 | stop="End Action" |
| 35 | ) |
| 36 | result = response['choices'][0]['text'].strip() |
| 37 | break |
| 38 | except Exception as e: |
| 39 | print(e) |
| 40 | max_try -= 1 |
| 41 | if max_try < 0: |
| 42 | result = "Exceed max retry times. Please check your davinci api calling." |
| 43 | break |
| 44 | return result, response["usage"] |
| 45 | |
| 46 | def add_message(self, message): |
| 47 | self.conversation_history.append(message) |
| 48 | |
| 49 | def change_messages(self,messages): |
| 50 | self.conversation_history = messages |
| 51 | |
| 52 | def display_conversation(self, detailed=False): |
| 53 | role_to_color = { |
| 54 | "system": "red", |
| 55 | "user": "green", |
| 56 | "assistant": "blue", |
| 57 | "function": "magenta", |
| 58 | } |
| 59 | print("before_print"+"*"*50) |
| 60 | for message in self.conversation_history: |
| 61 | print_obj = f"{message['role']}: {message['content']} " |
| 62 | if "function_call" in message.keys(): |
| 63 | print_obj = print_obj + f"function_call: {message['function_call']}" |
| 64 | print_obj += "" |
| 65 | print( |
| 66 | colored( |
| 67 | print_obj, |
| 68 | role_to_color[message["role"]], |
| 69 | ) |
| 70 | ) |
| 71 | print("end_print"+"*"*50) |
no outgoing calls
no test coverage detected