Skip to content

added openai models #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions OpenAi/ModerationResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class ModerationResponse
{
public string Id { get; set; }
public string Model { get; set; }
public List<ModerationResult> Results { get; set; }
}

public class ModerationResult
{
public Categories Categories { get; set; }
public CategoryScores Category_Scores { get; set; }
public bool Flagged { get; set; }
}

public class Categories
{
public bool Hate { get; set; }
public bool Self_Harm { get; set; }
public bool Violence { get; set; }
}

public class CategoryScores
{
public float Hate { get; set; }
public float Self_Harm { get; set; }
public float Violence { get; set; }
}
71 changes: 71 additions & 0 deletions OpenAi/OpenAiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Text.Json.Serialization;

public class OpenAIClient
{
private readonly string _apiKey;
private readonly HttpClient _httpClient;
private readonly string _dalleModel = "dall-e-2";
private readonly string _moderationModel = "omni-moderation-latest";

public OpenAIClient(string apiKey)
{
_apiKey = apiKey;
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", _apiKey);
}

public async Task<string> GenerateImageAsync(string prompt)
{
var url = "https://api.openai.com/v1/images/generations";
var requestBody = new
{
model = _dalleModel,
prompt = prompt,
n = 1,
size = "1024x1024"
};
var json = JsonSerializer.Serialize(requestBody);
var content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await _httpClient.PostAsync(url, content);
response.EnsureSuccessStatusCode();

var responseJson = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(responseJson);
var imageUrl = doc.RootElement.GetProperty("data")[0].GetProperty("url").GetString();

return imageUrl;
}
public async Task<ModerationResponse> ModerateContentAsync(string input)
{
var url = "https://api.openai.com/v1/moderations";
var requestBody = new
{
model = _moderationModel,
input = input
};
var json = JsonSerializer.Serialize(requestBody);
var content = new StringContent(json, Encoding.UTF8, "application/json");

var response = await _httpClient.PostAsync(url, content);
response.EnsureSuccessStatusCode();

var responseJson = await response.Content.ReadAsStringAsync();

var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

var moderationResponse = JsonSerializer.Deserialize<ModerationResponse>(responseJson, options);
return moderationResponse;
}

}
37 changes: 37 additions & 0 deletions OpenAi/OpenAiHandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import requests

class OpenAIHandler:
def __init__(self, api_key):
self.headers = {
"Authorization": f"Bearer {api_key}",
}

def generate_code(self, prompt: str) -> str:
payload = {
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.2, # lower temp for more focused code
}
headers = self.headers.copy()
headers["Content-Type"] = "application/json"

response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
response.raise_for_status()
data = response.json()
return data["choices"][0]["message"]["content"]

def transcribe_audio(self, audio_file_path: str) -> str:
headers = self.headers.copy()
with open(audio_file_path, "rb") as audio_file:
files = {
"file": (audio_file_path, audio_file, "audio/mpeg"),
}
data = {
"model": "whisper-1",
"language": "en"
}
response = requests.post("https://api.openai.com/v1/audio/transcriptions", headers=headers, data=data, files=files)
response.raise_for_status()
data = response.json()
return data["text"]