Skip to content

1.0.0-M1: run a demo must be implement a HttpMessageConverter #1209

Open
@totyumengr

Description

@totyumengr

Bug description
org.springframework.web.client.RestClientException: Error while extracting response for type [org.springframework.ai.openai.api.OpenAiApi$ChatCompletion] and content type [text/html;charset=UTF-8]

Environment
Java 17
spring-ai 1.0.0-M1
model gpt-4o

Steps to reproduce
Simply, write a controller like this:

    @GetMapping("/chat")
    public String chat(String query) {

        String answer = this.chatClient.prompt().user(query).call().content();
        return answer;
    }

Expected behavior

when use PostMan send a GET request like localhost:8080/chat?query=你好,
Error occurred.

I implement a class, convert response body to ChatCompletion.

@bean
public HttpMessageConverter<OpenAiApi.ChatCompletion> createMessageConverter() {

	final ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	return new AbstractHttpMessageConverter<OpenAiApi.ChatCompletion>(MediaType.TEXT_HTML) {

		@Override
		protected boolean supports(@SuppressWarnings("null") Class<?> clazz) {

			return ChatCompletion.class == clazz;
		}

		@SuppressWarnings("null")
		@Override
		protected ChatCompletion readInternal(Class<? extends ChatCompletion> clazz, HttpInputMessage inputMessage)
				throws IOException, HttpMessageNotReadableException {

			byte[] contentBytes = FileCopyUtils.copyToByteArray(inputMessage.getBody());
			ChatCompletion cc = mapper.readValue(contentBytes, clazz);
			return cc;
		}

		@SuppressWarnings("null")
		@Override
		protected void writeInternal(ChatCompletion t, HttpOutputMessage outputMessage)
				throws IOException, HttpMessageNotWritableException {
			throw new UnsupportedOperationException("Unimplemented method 'writeInternal'");
		}

	};
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions