microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
python/nprData/llm_util.py
125lines · modecode
| 1 | # Copyright (c) Microsoft Corporation and Henry Lucco. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | from groq import Groq |
| 5 | from dotenv import load_dotenv |
| 6 | from dataclasses import dataclass |
| 7 | from typing import List |
| 8 | import os |
| 9 | from openai import OpenAI |
| 10 | |
| 11 | @dataclass |
| 12 | class LLMTurn: |
| 13 | role: str |
| 14 | content: str |
| 15 | |
| 16 | def to_dict(self): |
| 17 | return { |
| 18 | "role": self.role, |
| 19 | "content": self.content |
| 20 | } |
| 21 | |
| 22 | class LLMClient: |
| 23 | def send_message( |
| 24 | self, |
| 25 | role: str, |
| 26 | content: str, |
| 27 | history: List[LLMTurn] | None = None |
| 28 | ) -> LLMTurn: |
| 29 | raise NotImplementedError("Subclasses should implement this method") |
| 30 | |
| 31 | # These classes ended up being the same but Anthropic and Perplexity both |
| 32 | # difference so keeping this way for now |
| 33 | class GroqClient(LLMClient): |
| 34 | def __init__(self): |
| 35 | groq_api_key = os.environ.get("GROQ_API_KEY") |
| 36 | if not groq_api_key: |
| 37 | raise ValueError("GROQ_API_KEY environment variable is not set") |
| 38 | |
| 39 | self.client = Groq( |
| 40 | api_key=groq_api_key |
| 41 | ) |
| 42 | |
| 43 | groq_model = os.environ.get("GROQ_MODEL") |
| 44 | if not groq_model: |
| 45 | raise ValueError("GROQ_MODEL environment variable is not set") |
| 46 | self.model = groq_model |
| 47 | |
| 48 | def send_message( |
| 49 | self, |
| 50 | role: str, |
| 51 | content: str, |
| 52 | history: List[LLMTurn] | None = None |
| 53 | ) -> LLMTurn: |
| 54 | messages = [LLMTurn(role, content)] |
| 55 | if history: |
| 56 | messages = history + [LLMTurn(role, content)] |
| 57 | |
| 58 | messages = [x.to_dict() for x in messages] |
| 59 | response = self.client.chat.completions.create( |
| 60 | model=self.model, |
| 61 | messages=messages |
| 62 | ) |
| 63 | return LLMTurn(role, response.choices[0].message.content) |
| 64 | |
| 65 | class OpenAIClient(LLMClient): |
| 66 | def __init__(self): |
| 67 | openai_api_key = os.environ.get("OPENAI_API_KEY") |
| 68 | if not openai_api_key: |
| 69 | raise ValueError("OPENAI_API_KEY environment variable is not set") |
| 70 | |
| 71 | self.client = OpenAI( |
| 72 | api_key=openai_api_key |
| 73 | ) |
| 74 | |
| 75 | openai_model = os.environ.get("OPENAI_MODEL") |
| 76 | if not openai_model: |
| 77 | raise ValueError("OPENAI_MODEL environment variable is not set") |
| 78 | self.model = openai_model |
| 79 | |
| 80 | def send_message( |
| 81 | self, |
| 82 | role: str, |
| 83 | content: str, |
| 84 | history: List[LLMTurn] | None = None |
| 85 | ) -> LLMTurn: |
| 86 | messages = [LLMTurn(role, content)] |
| 87 | if history: |
| 88 | messages = history + [LLMTurn(role, content)] |
| 89 | |
| 90 | messages = [x.to_dict() for x in messages] |
| 91 | response = self.client.chat.completions.create( |
| 92 | model=self.model, |
| 93 | messages=messages |
| 94 | ) |
| 95 | return LLMTurn(role, response.choices[0].message.content) |
| 96 | |
| 97 | class LLMChat: |
| 98 | turns: List[LLMTurn] |
| 99 | |
| 100 | # defaults to OpenAI |
| 101 | def __init__(self, client: str = "openai"): |
| 102 | client_map = { |
| 103 | "openai": OpenAIClient, |
| 104 | "groq": GroqClient |
| 105 | } |
| 106 | self.client = client_map[client]() |
| 107 | self.turns = [] |
| 108 | |
| 109 | def add_system_message(self, content: str): |
| 110 | self.turns += [LLMTurn("system", content)] |
| 111 | |
| 112 | def send_message(self, role: str, content: str) -> LLMTurn: |
| 113 | new_turn = LLMTurn(role, content) |
| 114 | response_turn = self.client.send_message( |
| 115 | role, |
| 116 | content, |
| 117 | self.turns |
| 118 | ) |
| 119 | self.turns += [new_turn, response_turn] |
| 120 | return response_turn |
| 121 | |
| 122 | if __name__ == "__main__": |
| 123 | load_dotenv("./env_vars") |
| 124 | client = GroqClient() |
| 125 | chat = LLMChat(client) |