Skip to content

Commit

Permalink
standardize llm inference code
Browse files Browse the repository at this point in the history
  • Loading branch information
William Nguyen authored and William Nguyen committed Sep 20, 2024
1 parent 85b7053 commit 16262a8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions inference_llm_openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
import openai
import argparse

parser = argparse.ArgumentParser(description="Inference for LLM OpenAI")
parser.add_argument('--api_key', type=str, help='Input your OpenAI API key')
args = parser.parse_args()
api_key = args.api_key

api_key = os.getenv("OPENAI_API_KEY") if api_key is None else api_key
client = openai.OpenAI(api_key=api_key)

def get_answer(prompt, model="gpt-4o", system_message="You are a helpful assistant."):
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content

if __name__ == "__main__":
prompt = "What is the capital of France?"
print(get_answer(prompt))

0 comments on commit 16262a8

Please sign in to comment.