Spaces:
Sleeping
Sleeping
File size: 2,411 Bytes
24c4def |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
from openai import OpenAI, AsyncOpenAI
import os
import base64
import httpx
import asyncio
# sk-jD8DeGdJKrdOxpiQ5bD4845bB53346C3A0E9Ed479bE08676
# https://oneapi.xty.app/v1
class SyncChat:
def __init__(self, model, api_key, base_url=None):
if base_url != None:
self.sync_client = OpenAI(base_url=base_url,api_key=api_key)
else:
self.sync_client = OpenAI(api_key=api_key)
self.model = model
def get_response(self, message, temperature=0.2, max_tokens=1024):
response = self.sync_client.chat.completions.create(
model=self.model,
messages=message,
temperature=temperature,
max_tokens=max_tokens)
return response.choices[0].message.content
class AsyncChat:
def __init__(self, model, api_key, base_url=None):
if base_url != None:
self.async_client = AsyncOpenAI(base_url=base_url,api_key=api_key)
else:
self.async_client = AsyncOpenAI(api_key=api_key)
self.model = model
async def get_response(self, messages,temperature=0.2,max_tokens=1024):
async def openai_reply(message):
response = await self.async_client.chat.completions.create(
model=self.model,
messages=message,
temperature=temperature,
max_tokens=max_tokens,)
return response.choices[0].message.content
response_list = [openai_reply(message) for message in messages]
return await asyncio.gather(*response_list)
class VisionChat:
def __init__(self, model, api_key, base_url=None):
if base_url != None:
self.client = OpenAI(base_url=base_url,api_key=api_key,http_client=httpx.Client(
base_url="https://oneapi.xty.app/v1",
follow_redirects=True,
),)
else:
self.client = OpenAI(api_key=api_key)
self.model = model
def get_response(self, message, temperature=0.2, max_tokens=1024):
response = self.client.chat.completions.create(
model=self.model,
messages=message,
# temperature=temperature,
max_tokens=max_tokens)
return response.choices[0].message.content |