48 lines
1.4 KiB
C++
48 lines
1.4 KiB
C++
#pragma once
|
|||
|
|
|
||
|
|
// OpenaiChatModel: llm::ChatModel over the OpenAI API in either dialect,
|
||
|
|
// the Responses API (default) or Chat Completions.
|
||
|
|
|
||
|
|
#include <functional>
|
||
|
|
#include <memory>
|
||
|
|
#include <string>
|
||
|
|
|
||
|
|
#include "llm/chat_types.h"
|
||
|
|
#include "openai/core/chat_completions.h"
|
||
|
|
#include "openai/core/responses.h"
|
||
|
|
#include "openai/core/wire/chat_wire.h"
|
||
|
|
|
||
|
|
namespace molo::openai::core {
|
||
|
|
|
||
|
|
enum class Dialect { kResponses, kChatCompletions };
|
||
|
|
|
||
|
|
struct ChatModelConfig {
|
||
|
|
Dialect dialect = Dialect::kResponses;
|
||
|
|
WireOptions wire;
|
||
|
|
};
|
||
|
|
|
||
|
|
class OpenaiChatModel : public llm::ChatModel {
|
||
|
|
public:
|
||
|
|
explicit OpenaiChatModel(std::shared_ptr<llm::ApiClient> client, ChatModelConfig config = {});
|
||
|
|
// OPENAI_API_KEY from llm::Config; not configured without it.
|
||
|
|
static std::shared_ptr<OpenaiChatModel> from_env(ChatModelConfig config = {});
|
||
|
|
|
||
|
|
std::string vendor() const override { return "openai"; }
|
||
|
|
llm::ChatResponse generate(const llm::ChatRequest& request) override;
|
||
|
|
void stream(const llm::ChatRequest& request,
|
||
|
|
const std::function<bool(const llm::ChatChunk&)>& on_chunk) override;
|
||
|
|
|
||
|
|
Dialect dialect() const { return config_.dialect; }
|
||
|
|
bool configured() const { return client_->configured(); }
|
||
|
|
|
||
|
|
private:
|
||
|
|
void check(const llm::ChatRequest& request) const;
|
||
|
|
|
||
|
|
std::shared_ptr<llm::ApiClient> client_;
|
||
|
|
ChatModelConfig config_;
|
||
|
|
ResponsesService responses_;
|
||
|
|
ChatCompletionsService chat_;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace molo::openai::core
|