Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2024
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. from openai import OpenAI as OAI
  2. from src.config import Config
  3.  
  4. class OpenAI:
  5. def __init__(self):
  6. config = Config()
  7. api_key = config.get_openai_api_key()
  8.  
  9. # Define the base URL for the OpenAI API
  10. base_url = "http://127.0.0.1:5000/v1"
  11.  
  12. # Create the OpenAI Client with the custom base URL
  13. self.client = OAI(
  14. api_key=api_key,
  15. base_url=base_url,
  16. )
  17.  
  18. def inference(self, model_id: str, prompt: str) -> str:
  19. chat_completion = self.client.chat.completions.create(
  20. messages=[
  21. {
  22. "role": "user",
  23. "content": prompt.strip(),
  24. }
  25. ],
  26. model=model_id,
  27. )
  28.  
  29. return chat_completion.choices[0].message.content
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement