OpenAI and Groq provider (OpenAI-compatible API).
| 87 | |
| 88 | |
| 89 | class OpenAIProvider(LLMProvider): |
| 90 | """OpenAI and Groq provider (OpenAI-compatible API).""" |
| 91 | |
| 92 | def __init__(self): |
| 93 | from openai import OpenAI |
| 94 | |
| 95 | if Config.LLM_PROVIDER == "groq": |
| 96 | self.client = OpenAI( |
| 97 | api_key=Config.GROQ_API_KEY, |
| 98 | base_url=GROQ_API_BASE_URL |
| 99 | ) |
| 100 | self.model = Config.GROQ_MODEL |
| 101 | else: |
| 102 | self.client = OpenAI(api_key=Config.OPENAI_API_KEY) |
| 103 | self.model = Config.OPENAI_MODEL |
| 104 | |
| 105 | def get_decision(self, goal: str, screen_context: str, action_history: List[Dict]) -> Dict[str, Any]: |
| 106 | history_str = format_action_history(action_history) |
| 107 | user_content = f"GOAL: {goal}\n\nSCREEN_CONTEXT:\n{screen_context}{history_str}" |
| 108 | |
| 109 | response = self.client.chat.completions.create( |
| 110 | model=self.model, |
| 111 | response_format={"type": "json_object"}, |
| 112 | messages=[ |
| 113 | {"role": "system", "content": SYSTEM_PROMPT}, |
| 114 | {"role": "user", "content": user_content} |
| 115 | ] |
| 116 | ) |
| 117 | |
| 118 | return json.loads(response.choices[0].message.content) |
| 119 | |
| 120 | |
| 121 | class BedrockProvider(LLMProvider): |